Usage of return statement


When I put return statement, that returns the value of the variable "cost" in the and of the function it gives me syntax error.

I've put return statements after the each if and elif blocks. I think the code block below should work correctly. Unlike my assumptions, it works incorrectly. As long as I put it in the end of the function, it works... But I think, in my method, the case which i put return statements after the each if and elif, it should work!

Replace this line with your code.

type or paste code here

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



At least for my code on where this def rental_car_cost is first made in 4.) Transportation in 8.) Taking a Vacation in Python my return statements are always in line with my elif and if statements so it would seem that you need to do the same with yours. For example when I have my code like yours:

    elif days >= 3 and days < 7:
        cost -= 20
        return cost

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

However, when I space the return statement in line with my elif I don't get any errors.

    elif days >= 3 and days < 7:
        cost -= 20
    return cost

I don't have a return for cost under my if so I don't have any examples of that.


That's right, I get the same output as you. When I delete spaces before the elif and align it with the elif statement, it totally works. But in the following case, program crashes:

1- aligning both return statements under the if and elif respectively will cause program crash. program only works when you delete the first return statement under the if. And align and keep only the second return statement under the elif.

So why does this happen? It is obvious that program only works with one return statement, which is aligned and not a member of any of these if and elif blocks. It should be completely independent of if and elif blocks and should be a member of the whole function. Can anyone explain this?


Well just from a practical stance you only want to print cost after all the expenses that could be or are have been figured so you want to return the cost only after your whole code. If you return the cost before your elif, you could potentially be returning the cost before any has been calculated. If you return a cost twice once after the if and once after the elif? Isn't that conflicting and redundant? If you show two costs then one of several circumstances will be in place: both will be correct or one will be correct and one will be wrong. Not even going into the fact it is an error with the two return costs in there which you have experienced. Hopefully that was a decent explanation for you and wasn't too poor. If that didn't help, I can see if another moderator or help desker can come explain it too you.


Thanks for your reply after all. But isn't it impossible? elif will only be executed if the 'if' statement is wrong. But if the 'if' statement is true, then there is no need to check elif. So that debugger will automatically exit both if and elif and get back to the body of the function right? I mean, no possibility for it to overwrite the value for the variable 'cost' twice. I hope I was able to explain at what point I stuck.


Ok I have an answer to your question but I am checking with a moderator just to make sure its right. I want to make sure I am not leading anyone astray with my answers from here on out xD. I will repost in here my answer or the answer the moderator give me here and I will notify you when I have done so :smiley:


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