Help! What's wrong with my code?


#1

Its instruction is:
1.Modify your trip_cost function definition. Add a third argument, spending_money.
2.Modify what the trip_cost function does. Add the variable spending_money to the sum that it returns.

And my code is:

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
    elif days>=3:
        cost-=20
    return cost
def trip_cost(days,city,spending_money):
    return hotel_cost(days)+plane_ride_cost(city)+rental_car_cost(days  )+spending_money

It reminds me that the code cannot distinguish the "str" and "Non type".But I don't know how ti deal with it.


#2

look at the error message:

Oops, try again.
trip_cost('Pittsburgh', 4, 0) raised an error: cannot concatenate 'str' and 'NoneType' objects

in particular the arguments order, the city is first, and the number of days second

now look at your function parameters, days is first (so days will contain value Pittsburgh) and city is second (so will contain value 4), this will cause massive problems for rental_car_cost, plane_ride_cost and hotel_cost given they are given values they can't handle


#3

I see. Thanks a lot. I didn't realise the importance of these arguments.


#4

for unnamed arguments the order is important


#5

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