Transportation


#1


https://www.codecademy.com/courses/python-beginner-P5YtY/1/3?curriculum_id=4f89dab3d788890003000096

Oops, try again. It looks like rental_car_cost returns None instead of the correct amount (100) for 3 days


Tried loads of different ways been stuck for ages. obviously theres something wrong. something not being defined as it comes back as none. tried defining days didnt seem to work. tried defining cost different also tried doing it like in the previous tasks. im doing something wrong!!

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


#2

well, if we can add a function call:

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

print rental_car_cost(x)

where x is the number of days. You will see that for one or two days, the correct amount is returned. Not for other days

You always need to return cost

when the if condition is true, elif and else won't execute


#3

i added the print function at the end. Still comes back with the same message.


#4

the print statement was for you to show you when a return keyword is (or isn't reached), so you can see where you go wrong (by changing x into the number of days (for example 4))

Of course if you adding the print statement will still give you an error message, as i explained in my answer.

Help != instant solution


#5

So elif statement needs to be executed but i dont know how. Whats the Help != instant solution about?


#6

the elif statement executes, but it doesn't return anything.

That the forum is for helping, not getting instant solutions. Your reply:

suggested adding a print statement would instantly fix your problem


#7

Not asking for you to give me a answer. Just some help!! Im not a developer. Just wanted to know why my elif statment doesnt return anything and why the rest of my code doesnt work.


#8

because a return keyword is only reached when else runs


#9

you can do it with only one return statement, but you need to take return out of the conditional block by lining it up with the if and elif:

if a:
then b
elif c:
then d
return

also make sure your cost statements are correct and your indentations are exactly 4 spaces


#10

don't indent the return is what I meant to say.


#11

this approach would work if he update cost. @jhcroydon you have two options, or add return inside if and elif or update cost and then return cost in the end (without else)


#12

Yes, thank you for that.


#13

what do you mean lining it up with if and elif?


#14

If you look at the example with tickets you will see what I mean. The if, elif, and return are aligned left (not indented).

if:
    then
elif:
    then
return

#15

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