Taking a Vacation (4. Transportation) Help please!


#1

This is my code for this part. I get the error "It looks like rental_car_cost returns 120 instead of the correct amount (100) for 3 days."

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

Thanks!


Need help taking a vacation (transportation)
#2

Read your code line by line, execute it in your head.
The error complains about days=3, so use that when you do this.

Be careful not to do what you expect/want your code to do. Read the code and execute it, like a machine.

You can also add temporary prints in your function to see what happens. I find it best to make those prints print out natural language, so that just by reading the input, without knowing anything about coding, you can tell how it goes wrong.


#3

The following is my code. I still get error. I can not figure out what I am entering wrong. thanks beforehand

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

#4

Please format your post as I have done (select entire code block; press Ctrl+Shift+C) from next time. Thanks! :smile:

As @ionatan clearly told you, you need to execute the code in your head line by line. By this logic, one can clearly see that the problem is on the second line - return 40 * days. You are returning, and thus exiting from the function (and none of the following statements are executed), with a return value of 40 * days.Now, hints:

  1. You do not have to exit from the function at that point.
  2. You are referring to cost variable below. And you are missing it's definition. It should be on line 2.

Can you now correct your code?


#5

I use mac and therefore just Command +C and V-ing for pasting but anyway.

Thanks for your help. Appreciated!


#8

i think it doesn't work because "cost" is not defined, define cost and it should go. not an expert though, so i could be wrong


#11

your code will end when it executes the second line “return 40 * days "


#13

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

hope makes sense, i am new at this as well, so is hard to explain