4.transportation


#1

Hello i'm currently at "Transportation". And i'm stuck with my code.

def hotel_cost(nights):
# 140 per nacht
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):
price == 40
if days >= 7:
price -= 50
elif days >= 3:
price -= 20
return price

The error i am getting is File "python", line 15
if days >= 7:
^
IndentationError: unindent does not match any outer indentation level

Could anyone explain this to me ?:smiley: Thanks alot!


#2

Just as your error says you have messed up indentation. In python syntax it is SUPER important that you correctly indent your code.

As you can see in the following after each line if the code is a continuation there is 4 spaces multiplied by the continuation level.

The means that if you have a whole bunch of statements that can folllow each other do not start indenting them unless they need it like if statements.

Example:

var = 'Do not indent'
for i in var:
    print("You have to indent this line or you will get syntax errors!
print("Though you do not have to indent this line uness you want it in the for loop!")

EXAMPLE:

def hotel_cost(nights):
    cost_nights = nights * 140
    return cost_nights
    
def plane_ride_cost(city):
    city = city.lower
    if city == 'charlotte':
        plane = 183
    elif city == 'tampa':
        plane = 220
    elif city == 'pittsburgh':
        plane = 222
    elif city == 'los angeles':
        plane = 475
    else:
        print 'You have entered an invalid city name'

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

#3

Oh so i just missed a few space or tabs. Now i have updated my code to work more proptely

def hotel_cost(nights):
# 140 per nacht
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:
cost = 40 * days-50
if days >= 3:
cost = 40 * days-20
return cost

But now im getting this error of . It looks like rental_car_cost returns 260 instead of the correct amount (230) for 7 days. '' Dunno how to fix it really. :frowning:


#4

fixed by doing
cost = days*40
if days >= 7:
return cost-50
if days >= 3:
return cost-20
return cost