5/7 Pull it together (Taking a Vocation)


#1

What is wrong?

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 = days * 40 
    if days >= 7:
        return cost -50
    elif days >= 3:
        return cost -20
    else:
        return cost

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

Message: Oops, try again. trip_cost('Los Angeles', 8) raised an error: global name 'nights' is not defined


#2

here:

hotel_cost(nights)

where does nights come from? this should be days


#3

It is come from def hotel_cost(nights)
Why it should be days ?


#4

but nights is the function parameter of hotel_cost:

def hotel_cost(nights):
    return 140 * nights
print hotel_cost(5) # will print 700
print nights # will give an error.

nights only exist inside the hotel_cost function, so using it outside the function will cause an error.

so in your trip_cost function:

def trip_cost(city, days):
     return hotel_cost(....)

you have choice of 2 variables, days or city (trip_cost parameters)


#5

Ok i'm make choice "city" and it's telling me "Oops, try again. Make sure to define a function named trip_cost."
Choose "days" and the same message.
From the example i'm trying to "return the sum".
What's a point?


#6

this is what you had:

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

then i tried to explain that nights won't work (variable scope), so you should use days instead. Then it should be good?

return 183 is wrongly indented, fix the indent and you should be good


#7

No, it won't work.

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 = days * 40 
    if days >= 7:
        return cost -50
    elif days >= 3:
        return cost -20
    else:
        return cost

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

Message: Oops, try again. Make sure to define a function named trip_cost.


#8

covered this problem?


#9

I had no such problem.


#10

look at it:

    if city == "Charlotte":
    return 183

after a if condition there should be indention:

    if city == "Charlotte":
         return 183

which isn't in your case


#11

I have 4 space before return 183


#12


#13

okay, can you post an updated version of your code then? Please use markup:

So the indent shows properly, i can rule out this problem


#14

Asking me to define when it is already defined.

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 = days * 40 
    if days >= 7:
        return cost -50
    elif days >= 3:
        return cost -20
    else:
        return cost

    def trip_cost(city, days):
        hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days)

#15

the trip_cost function is currently part of rental_car_cost function, it shouldn't be. it should be outside/after rental_car_cost function (hint: indention)

also, your trip_cost function doesn't return anything


#16

Moved it left side.
Now message: Oops, try again. trip_cost('Los Angeles', 3) returned None instead of the correct value 995


#17

you read 50% of my answer, if you need to return something, use the return keyword


#18

Yes, it's worked.

But.
About hotel_cost(nights).
We have hotel_cost with argument (nights). So why it shoud change to days? Instruction asking us to sum rental_car_cost(days), hotel_cost(days), and plane_ride_cost(city).
From lessons before we dont have hotel_cost(days).
Why we should change nights to days?


#19

we don't change nights to day, here:

def trip_cost(city, days):
        hotel_cost(days) + plane_ride_cost(city) + rental_car_cost(days)

we call the hotel_cost function with days as argument, but the parameter of hotel_cost is still night.


#20

Oddly.
Ok, i should read more about it to prevent mistakes like this in future lessons.
Thnaks.