Taking a Vacation : Transportation


#1

Can someone help me?I'm writing the last part of my code about transpotation, but i dont know whats the problem ,whe i run it ,returns ''Oops, try again. An error was raised for rental_car_cost(3): local variable 'off' referenced before assignment''

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
    else:
        return "There's not available flights for this city"
        
def rental_car_cost(days):
    cost = 40 * days
    if days >= 7:
        off += 50
    elif days >= 3:
        off += 20
    return cost

#2

The error itself is telling you what the problem is. You attempt to make changes to the off variable before you actually tell the program what it is.

The lesson wants the cost to be a certain price, except under a few conditions. So the cost itself is what you should be modifying instead of trying to make a different variable.


#3

ahh thank u ! i understand that it has to be less ,because its off! but i saw 2 different ways to do it and it works but what's the difference between them?the first one seens to be better because there's an else ,and the other there's not.
first one:

def rental_car_cost(days):
    cost = 40 * days
    if days >= 7:
        return cost - 50
    elif days >= 3:
        return cost - 20
    else:
        return cost

and

second one:
def rental_car_cost(days):
    cost = 40 * days
    if days >= 7:
        cost -= 50
    elif days >= 3:
        cost -= 20
    return cost

#4

Truthfully it is personal preference. There is no real requirement to have an else statement and if you don't need something then why do it?

I personally would use the model:

return cost - 50

The reason being is that I want it to return the cost minus 50. So if the cost were 200 and I had multiple instances were I needed print the cost minus a certain amount it would be consistent. Such as:

cost = 200
print cost - 50
print cost - 20
print cost - 35
print cost - 80

would return:

150                                                                                                                                                                             
180                                                                                                                                                                             
165                                                                                                                                                                             
120

However, if I use -=, being an assignment operator, it changes the stored value of cost. Such as:

cost = 200
print cost
cost -= 50
print cost
cost -= 20
print cost
cost -= 35
print cost
cost -= 80
print cost

Would return:

200                                                                                                                                                                             
150                                                                                                                                                                             
130                                                                                                                                                                             
95                                                                                                                                                                              
15

#5

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

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

where is the mistake it is saying"" trip_cost('Los Angeles', 9) raised an error: 'int' object is not iterable""
Help me


#6

This is a good way to think about the difference between calculating it each way and why. I appreciate you sharing this for all.


#7

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