4 Transportation


#1



4/7 Transportation


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


I don't know why this code doesn't execute.


Replace this line with your code.


#2

Can you include your full code with indentation? Are you calling rental_car_cost(n)?


#4


#5

What happens if the number of days you rent a car is 2? :slight_smile:
It looks like that function wouldn't return.


#6

Thanks! I didn't think about that situation!!:grin:


#7

You're welcome!

A good practice is to put a standard return statement outside of any loop you are working in, just in case. :slight_smile:

For this particular example, putting a return cost statement outside of the for loop and at the end of the function will take care of the error.

Happy coding!


#8

It didn't work for me for some reason. It returns "Oops, try again. Did you create a function called rental_car_cost?"

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 47

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

#9

Your code is right by otherwise, but you should not return cost inside the else- statement. So after your last elif, just

return cost

Did you see? Magic is true! Okay maybe not. Your else statement confused the thing, so the cost- variable had a value but it returned nothing. :slight_smile:


#10

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