Need Explanation of Why We Use +=/-=....Is is because we need to use lists?


#1



https://www.codecademy.com/courses/python-beginner-P5YtY/1/3?curriculum_id=4f89dab3d788890003000096


My code is actually behaving correctly- I knew I had to write it this way because of the example given on the page. I do not understand why we need to use the "+=" and the "-=" in the transportation (car rental cost) section.

Is this because we need to make this into a list? Is do we need to direct the computer to maintain the cost argument before altering it? I'm not sure why we can't simply write cost - 50 or cost - 20? Why is this so?


def hotel_cost(nights):
    return 140 * nights

def plane_ride_cost(city):
    if city == "Charlotte":
        return 183
    elif city == "Tampa":
        return 220
    elif city == "Pittsburgh":
        return 222
    elif city == "Los Angeles":
        return 475
        
def rental_car_cost(days):
    cost = days * 40
    if days >= 7:
        cost -= 50
    elif days >= 3 and days < 7:
        cost -= 20
    return cost


#2

Hi the

cost -= 50

is the same as

cost = cost - 50

the same for +=


#3

Thank you! To clarify, to make I am understanding this 100%....

By using the -= and the += in this instance, I am merely telling python that it should look at this line of code as though cost is actually on either side of the operator? This is the shorthand way to do it?

Thank you so much!


#4

This is the shorthand way to do it?
yes


#5

You're amazing. Thanks!


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.