Taking a Vacation - Plan your trip


#1

It's printing, but keeps erroring saying: "Oops, try again.
trip_cost should take exactly three parameters: city, days, and spending_money (in that order).", but they're in that order and I can't figure this out. Please assist.

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 = 40 * days
    if days >= 7:
        cost -= 50
        return cost
    elif days >= 3:
        cost -= 20
        return cost
    
def trip_cost(city, days, spending_money):
    return plane_ride_cost(city) + rental_car_cost(days) + hotel_cost(days) + spending_money
    
print trip_cost("Los Angeles", 5, 600)

#2

rental_car_cost(days) does not have a return statement if "days" cannot pass the conditional checks


#3

Removing the 'return's in that statement, gives me this error:

Traceback (most recent call last):
  File "python", line 25, in <module>
  File "python", line 23, in trip_cost
TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'

and I still get the above error.


#4

Sorry, I mightve worded that poorly. Imagine what would happen if days in that method were only 2. It's not >= 7 so it would skip the first if, then it's not >=3 so it would skip that elif. Functions need to return no matter what path is taken, and in the case I gave, it can't return. You need an extra return statement in there in case both the if and elif are skipped. Or to save code, you could do something like this:

def rental_car_cost(days):
    cost = 40 * days
    if days >= 7:
        cost -= 50
    elif days >= 3:
        cost -= 20
    return cost

Now your conditions still work, and no matter what happens the function will return a value.


#5

That did it! Thank you!