Hey, you never know!


Hey, you never know!

Oops, try again. trip_cost('Los Angeles', 8, 0) returned 1165.0 instead of the correct value 1865

I'm not sure why I get this message. When I run the code in python 3 it looks like everything works fine.

def hotel_cost(nights):
    return 140 * nights
nights=raw_input('How many nights will you be staying? ')
print('That will cost=> $'+ str(hotel_cost(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

        return 'We do not fly there!'
city=raw_input('Enter a city destinaton: ')
city= city.title()#capitalize first letter of each word

print('That will cost=> $' + str(plane_ride_cost(city)))

def rental_car_cost(days):
    if days >= 7:
        return days*40-50
    elif days >=3:
        return days*40-20
        return days*40
days=float(raw_input('How many days will you need a rental car? ') )       
print('This will cost => $' + str((rental_car_cost(days))))

def trip_cost(city,days,spending_money):
    return rental_car_cost(days) + hotel_cost(nights) + plane_ride_cost(city) + spending_money
spending_money=float(raw_input('How much are you spending on other things? '))    
print('The total cost is => $' + str(trip_cost(city,days,spending_money)))


Hi @wchiment,

This statement within the trip_cost function is incorrect ...

    return rental_car_cost(days) + hotel_cost(nights) + plane_ride_cost(city) + spending_money

You are passing a global variable, nights, to the hotel_cost function, when you should be passing it the local days variable instead.


That works, but isn't that calculating the the cost of car rental days? So it doesn't matter what is put into the variable nights for the hotel_cost because it isn't used for the trip_cost?


Within the trip_cost function, the same number needs to be passed to the hotel_cost function as is passed to rental_car_cost function. That number represents the duration of the trip in days. That trip duration is specified by the value that is passed as the second argument in a call to the trip_cost function. Since the second parameter of the trip_cost function is days, the value of the second argument in a call to that function gets assigned to days. Therefore, days is the appropriate variable to pass on to both the rental_car_cost function and the hotel_cost function.

If you pass nights to the hotel_cost function within the trip_cost function, you are bypassing the days parameter that holds the information that is needed to compute the cost of the hotel stay.


Thank you for the insight