Please help on Taking a Vacation (Transportation)


#1

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 = days*40
    if days >= 7:
        cost -= 50 
    elif days >= 3 and days < 7:
        cost -= 20
        return cost
this is my code and i keep getting this error

"oops try again, it looks like rental_car_cost returns None instead of the correct amount (40) for 1 days
please help!


#2

you might want to re-indent your return cost, it currently only returns if elif is true, i would put the return at the same indention level as elif, this way something always gets returned.


#3

Hi There,

Because you are missing 'else' statement at the end of your rental_car_cost function to return just cost if not condition is match. Give it a shot!


#4

why not simple re-indent the return cost at the same indent level as if and elif? This way, any discount will be given, and then the correct number is returned, no else needed, i think it is actually a nice and valid shorthand (if at least indented correctly)


#6

Once your return something form the function, function stops executing the following codes. In reported code, the return is missing under if statement, it's indented fine under elif statement. But what about if none of the condition specified by if and elif statement does not match. so you have to add return outside if loop. Agree, that you don't need to specify the else statement and can just put the return outside the if loop and it would work fine. Hope this makes sense. Something like below code.

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

OR

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

#7

what is wrong with:

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

? It is less code, the cost variable gets updated if a discount is required, and then gets returned. It is less lines of code, and given dubkingskrillex code the best solution. I don't see why i should add this many more returns, while i could just make it work with one small re-indention


#8

Hi steim94, You are correct. You rock! I did not think about it. Just re-indenting the return out side the if loop makes perfect sense. May be I was overthinking it.


#9

your solutions also work, and they are not wrong, but this is by far the shortest way to help this person :slight_smile: