Taking a Vacation. Transportation



File "python", line 17
SyntaxError: 'return' outside function

It is telling me an error That I think is not correct at all. I need help I am sure the line is in the function but it tells me otherwise.

Replace this line with your code. 
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

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


All of those lines need four additional spaces in the indent.


Thanks for letting me know. I just wanted to be sure what do you mean by additional spaces? I am new to coding and well I am new to some of the lingo.


Python relies upon indentation to determine the scoping and context of code blocks.


Not needed since the if branch covers 7 or more. This is an elif so the first branch will have been bypassed already for values less than 7. Only values less than 3 will bypass the elif branch.

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


OK thanks for that explanation.


I checked the lesson and my code had the spaces but it had the same problem.


Indentation must be consistent and not ragged. It should not contain mixed spaces and tabs. For best assurance, redo every indent with either four or eight spaces, depending the depth of the block.


ok thanks i will do that.


It gave me the same answer.


Please post your code in a reply. Thanks.


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


Won't get put out right but I do have the return's indented.


But not the function block, itself. cost needs one more space in front, and the if statement needs to indented to line up with that. The returns will be indented further, still. See my example, above.


Thanks it worked and now I can go to next lesson. I


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