5 pull it together error.... plane_ride_cost('Charlotte') raised an error: global name 'price' is not defined


#1
def hotel_cost(nights) :
    return (140*nights)
    

def plane_ride_cost(city) : 
    return price
    
    if city == "Charlotte" :
        return "183"
    
    elif city == "Tampa" : 
        return "220"
    
    elif city == "Pittsburgh" :
        return "222"
        
    elif city == "Los Angeles" :
        return "475"
        
    else :
        return "none"
        
def rental_car_cost(days):
    cost = 40 * days
    if days >= 7:
        cost -= 50
    elif days >= 3:
        cost -=20
    return cost
    
print(rental_car_cost(5))


def trip_cost(city, days) :
    return (rental_cars_cost(days) + hotel_cost(nights) + plane_ride_cost(city))

#2

In this post, in your browser, press Ctrl+F (search for text) and type price. Look at what gets highlighted.

When you post code, make sure to mark it as prefromatted text, otherwise it gets mangled by markdown and is no longer valid code, it's different from your own code and that's a huge problem if you want to ask about your own code.


#3

I got the highlighted text of price but i still can't seem to solve the problem. Is the code itself wrong ?


#4

The error message says that you are using a variable that has not been defined.
Highlighting all occurrences of that variable shows you all places where it is defined, and all places where it's used.

If you are using it and you have not defined it, then you need to either define it or stop using it.


#5

Please format your post as I have done (select entire code block; press Ctrl+Shift+C) from next time. Thanks! :smile:

In the above line, you are returning price which isn't defined in your method till that point. So, Python looks outside of that method (in the global scope) and still finds no occurrence of price variable. Therefore, you get such error.

It should be clear by now how to fix this error.


#6

Hello, hope my code helps!

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=cost-50
elif days>=3 and days<=6:
cost=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)