Plan Your Trip NameError


#1



def hotel_cost(days):
    return 140 * days
    
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(city, days, spending_money):
        return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days) + spending_money
    
print trip_cost("Los angeles", days == 5, spending_money == 600)



(error)
Traceback (most recent call last):
  File "python", line 25, in <module>
NameError: name 'days' is not defined


#2

your function call:

print trip_cost("Los angeles", days == 5, spending_money == 600)

just pass the arguments, they are copied into the function parameters of trip_cost.

just pass 5 as argument, not need to do a comparison

same for spending money


#3

ive already changed the code too much, and done this.

def hotel_cost(days):
    return 140 * days

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(city, days, spending_money):
        return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days) + spending_money
    
        return trip_cost("Los Angeles", 5, 600)
print trip_cost

its now not printing the right number


#4

doesn't this cause an error? maximum recursion depth reached?

this is the function call:

trip_cost("Los Angeles", 5, 600)

now all you need to do is place the function call outside the function


#5

i got rid of the return as you said and i still get the same error,


None


None
Oops, try again. It looks like the correct value (1955) was not printed.


#6

i didn't say that, i said you should place the function call outside the function (the code is only to show what the function call is)

Don't only look at the code, also look at what i write


#7

that just causes an unindent error doesnt it?


#8

it shouldn't. Can i see your code again?


#9

def hotel_cost(days):
return 140 * days

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(city, days, spending_money):
return hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days) + spending_money

return trip_cost("Los Angeles", 5, 600)

print trip_cost


#10

you have to remove the indent of the function call completely, to place it outside the function.

Also, return can't exist outside a function.


#11

do you have the correct code now? if you do, what did you do?


#12

If you have a question, go here and make a new topic


#13

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