4 - taking a vacation:transportation

Please can someone help me, as I don’t know how this code has gone wrong:

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

Hi, @mightybraveknight ,

The code that you posted is fine, But there might be a problem in the part of your code that you did not post, namely the hotel_cost function or the plane_ride_cost function.

I’ve just done the exercise:

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

‘cost’ represent ‘price’ in your case

1 Like

i dont understand the “-=” part. Maybe it’s the language barrier …

x -= a is a short way of saying x = x - a
x += a is the short way of saying x = x + a

both ways work just fine

2 Likes

why did you put the “days>=7” in parenthasis??

that was a mistake on my part, thank you. fixed it now

oh ok. im also having a problem with my code if you could please take a look at it

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:
        cost -= 50
elif days >= 3:
        cost -= 20
        return cost 

the only problem i see in your code is the indentation of the last return that actually has to be on the first line. cause you return the cost after you define the functions. where you’ve put it, is actually inside the def rental_car_cost(days) function.

1 Like

hope that makes sense

so just push it back to the beginning of the line?

or do i need to put it on a different line altogether??

hmm…wait i think i am wrong. i am new at this also…and i made the exercise a few days back…please let me check again

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):
        cost -= 50
    elif (days >= 3):
        cost -= 20
    return cost

This was my solution at the time, again sorry for the parentheses’

1 Like
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:
        cost -= 50
    elif days >= 3:
        cost -= 20
    return cost 

I got it! thanks for the help #cristisilas
can someone explain how indentation works?

the indentation is parent and children.

1 Like

witch means… each child has to go under the parent??

exactly. the ones on the same line has the same relation. all 3 def func() in our case have the same relation, then the if/elif statements are the children and so on

1 Like

i think i get it
thanx!!

you are welcome, maybe in a few classes you will help me with something :slightly_smiling:. tomorrow i will try to start the python class again, i had to take a short break but is so cool i cant wait to go back. so i will see you around