Transportation (Returns None Error)


#1



Error:
Oops, try again. It looks like rental_car_cost returns None instead of the correct amount (40) for 1 days.


can't pinpoint what's wrong with this one. I thought everything I did was right.


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


#2

I believe that they want you to subtract 50 from the total amount if the number of days is greater than or equal to 7.


#3

now whatever I do, I get that same error.
tried all remedy I see in other threads.
I also refreshed the whole page, but it's still giving me the same error.


#4

Hi

I did my code different to yours, instead of cost I used days.

def rental_car_cost(days):
if days >= 7:
return 40 * days - 50
........etc, etc

I also tried cost = 40 * days and got the same response. I changed my approach and focused on days and left cost completely out of my code.

It worked and I added a print command at the end - print rental_car_cost(add any number here) to see if it actually did the calculations correctly. Of course I did first work out what the cost would be before hand on a piece of paper to make sure it actually did the calculations correctly.

I hoped this helped, being a beginner myself I know how frustrating it is to think outside the examples that are provided for us.


#5

Thanks! Guess I should also start thinking of different approaches rather than just plainly follow them.


#6

Glad I could help. I will admit that my other approach was not immediate, it took some time to puzzle it out.


#7

try this

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


#8

having the same issue as above, can you explain why this works?


#9

What issue are you having and can I see your code?


#10

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

I have the correct indents. I found that adding "return cost" as an else statement fixes the problem. I am wondering why that is


#11

That is because if the days driven is neither equal to the if or elif statements(for example if days=1) it should just return the cost. Adding that else statement will fix your problem.


#12

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