Stuck on Transpo


#1

This is the error I'm getting: Oops, try again. It looks like rental_car_cost returns 120 instead of the correct amount (100) for 3 days


f hotel_cost(nights):
    return 140 * nights
def plane_ride_cost(city):
    if city == "Charlotte":
        return int(183)
    elif city == "Tampa":
        return 220
    elif city == "Pittsburgh":
        return 222
    elif city == "Los Angeles":
        return 475
def rental_car_cost(days):
    total= days * 40
    return total
    if days >= 7:
        cost= -50
    elif days >= 3:
        cost= -20
    return cost


#2

It's always returning days * 40 because the instant the code sees a return, exits and ends the function. Think of a situation where it would go to the if statement? (never).

Change the code so you set the preliminar cost (or total, or whatever you want to call it) to days*40 and right there, if necessary, make the discounts to the same variable (just like you did) and return it at the very end.

I'm not sure I was clear, let me know if it worked!


#3

I took out return total and still got the same error.


#4

Ok, but that's because of another reason. Maybe share your updated code if you're still stuck?

Be sure you're calling (or using) the correct variables, you have days that is sent as a parameter, then you have total and cost.
You need only two variables. The one that's given to you as a parameter and the one you'll return after all the math and logic is applied. Good luck!


#5

This code works but why does it need the minus and equal sign to work? Wouldn't cost - 50 produce the same result?

def hotel_cost(nights):
    return 140 * nights
def plane_ride_cost(city):
    if city == "Charlotte":
        return int(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:
        cost -= 20
    return cost

#6

because cost - 50 would just temporary update cost, we can see this nicely when using print:

i = 10
print i # 10
print i + 5 # 15
print i # 10
i = i + 5 # or i += 5, += is a shorthand
print i # 15

without the equal sign the variable will not be updated, we really need to update (re-assign) cost.


#7

Exactly, += or -= means that i = i + something (or i = i - something).


#8

Thanks for explaining that.


#9

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