4 - taking a vacation:transportation


#1

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

#2

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.


#7

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


#8

i dont understand the "-=" part. Maybe it's the language barrier ...


#9

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


#10

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


#11

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


#12

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

#13

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.


#14

hope that makes sense


#15

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


#16

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


#17

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


#18

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'


#19

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?


#20

the indentation is parent and children.


#21

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


#22

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


#23

i think i get it
thanx!!


#24

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