Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Unsupported operand type(s) for +=: 'int' and 'datetime.timedelta'

This is the error im getting:

Exception has occurred: TypeError
unsupported operand type(s) for +=: ‘int’ and ‘datetime.timedelta’

I have searched for other people with similiar problems, but couldn’t quite find anything to similiar to mine. I havent gotten this problem before either.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I want to calculate the total flight time

The problem occurs in this line of code:

def get_total_flight_time(self):

        total_flight_time = 0

        for f in self.flights:

            total_flight_time += f.get_flight_time()


        return total_flight_time

Here is the rest of my code for reference

from datetime import datetime

class Flight(object):
    def __init__(self, new_flight_number, new_departure_time, new_arrival_time):
        self.flight_number = None
        self.departure_time = None
        self.arrival_time = None

        self.flight_number = str(new_flight_number)
        self.departure_time = new_departure_time
        self.arrival_time = new_arrival_time

    def get_flight_number(self):
        return self.flight_number

    def get_departure_time(self):
        return self.departure_time
    def set_departure_time(self, new_departure_time):
        self.departure_time = new_departure_time

    def get_arrival_time(self):
        return self.arrival_time
    def set_arrival_time(self, new_arrival_time):
        self.arrival_time = new_arrival_time

    def get_flight_time(self):
        return (self.arrival_time - self.departure_time )

class Itinerary(object):
    def __init__(self, new_flights):
        self.flights = None

        self.flights = new_flights
        
    def get_total_travel_time(self):
        total_travel_time = 0
        if len(self.flights) > 0:
            first_flight_departure = self.flights[0].get_departure_time()
            last_flight_arrival = self.flights[len(self.flights) - 1].get_arrival_time()

            total_travel_time = (last_flight_arrival - first_flight_departure )
        
        return total_travel_time

    def get_total_flight_time(self):

        total_flight_time = 0

        for f in self.flights:

            total_flight_time += f.get_flight_time()


        return total_flight_time


def main():

    flights = []

    flights.append(
        Flight("US230", datetime(2014, 4, 5, 5, 5, 0), 
                        datetime(2014, 4, 5, 6, 15, 0))
    )

    flights.append(
        Flight("US235", datetime(2014, 4, 5, 6, 55, 0), 
                        datetime(2014, 4, 5, 7, 45, 0))
    )

    flights.append(
        Flight("US237", datetime(2014, 4, 5, 9, 35, 0), 
                        datetime(2014, 4, 5, 12, 55, 0))
    )

    itinerary = Itinerary(flights)

    print("Total Travel time: ", itinerary.get_total_travel_time())

    print("Total Flight time: ", itinerary.get_total_flight_time())

main()

>Solution :

The reason behind this exception is since total_flight_time (int) and f.get_flight_time() (returns a timedelta) are two different types, Python does not know how to apply the + operand to them. In order to resolve this, you’ll want to make sure they are the same type before you add them, namely, either int or datetime.timedelta. It seems like you need the number to be a timedelta object, so in order to create one with a value of 0, use timedelta(seconds=0), and assign this value to total_flight_time.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading