8. Taking a Vacation 4. Transportation


#1

I'm having a little bit of trouble with my code, it's returning a syntax error but I can't sort out where the problem is coming from. I've browsed the forums but haven't seen any topics related to my issue. If someone could tell me what I'm doing wrong and maybe help optimize some of my code, I would be greatly appreciative.

The lesson I'm in is notated in the title

Here's my code:

def hotel_cost(nights):
    return 140 * nights 
    
def plane_ride_cost(city):
    if city == "Charlotte":
        return 183
    if city == "Tampa":
        return 220
    if city == "Pittsburgh":
        return 222
    if city == "Los Angeles":
        return 475

def rental_car_cost(days):
    if days <= 2 :
        return 40 * days
    elif days == 3 or days == 4 or days == 5 or days == 6:
        return 40 * days - 20
    else days >= 7 :
        return 40 * days - 50

I'm getting the following error message:

 File "python", line 19
    else days >= 7 :
            ^
SyntaxError: invalid syntax

I thought I had proper indentation and use of parentheses, but I'm totally lost on what might be causing this.

Any help would be appreciated.

Thanks.


#2

else is everything else. There's no condition to that.


#3

Hey, I changed the else to elif and added an else at the end and it went through.

code now looks like this and it works:

def hotel_cost(nights):
    return 140 * nights 
    
def plane_ride_cost(city):
    if city == "Charlotte":
        return 183
    if city == "Tampa":
        return 220
    if city == "Pittsburgh":
        return 222
    if city == "Los Angeles":
        return 475

def rental_car_cost(days):
    if days >= 7 :
        return 40 * days - 50
    elif days <= 2 :
        return 40 * days
    elif days == 3 or days == 4 or days == 5 or days == 6:
        return 40 * days - 20
    else : return "error"

Thanks for your help! :slight_smile:


#4

You can write this better : elif days == 3 or days == 4 or days == 5 or days == 6:

elif days >= 3:


#5

Taking a vacation know the transportation cost

def rental_car_cost(days):
car_rent = 40 * days # renting cost per day $40 as example
if days >= 7:
car_rent -= 50 # 7 or more days discount $50 off
elif days >= 3:
car_rent -= 20 # 3 or more days discount $20 off
return car_rent

And voila


#6

Why do you write -= instead of just - when subtracting the offers from the final cost?


#7

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