4. Transportation


#1



https://www.codecademy.com/en/courses/python-beginner-P5YtY/1/3?curriculum_id=4f89dab3d788890003000096#


Oops, try again. It looks like rental_car_cost returns None instead of the correct amount (100) for 3 days.


I'm not sure what to try next. I've modified just about everything I can think of and still no luck, now I've reverted back to my original code which is what I beleive to be correct. It mimics the structure of the example 1:1 so I'm not sure what's wrong.

Let me explain what I think is going on for learning purposes and so I don't just get an answer without knowing why.

  1. I'm defining a function called rental_car_cost with an argument of (days)
  2. Within it, I'm creating a new variable called totalCost and setting it equal to 40 times the number of days.
    3.If the number of days is greater than or equal to 7, deduct 50 from totalCost. If not, but is greater than or equal to 3 days, deduct only 20 from totalCost.
    4.Otherwise, return the totalCost. (Note: I tried adding an else: above return as I felt it could be the problem, but that didn't work.

Also, I've tried adding rental_car_cost(5) above and below return totalCost (even though example doesn't have them) because I don't understand how the code will work without it. This line will set the number of days so without it, how will the rest of the function know how to calculate totalCost? This didn't work when adding, as you probably guessed.

Another side note: The error message seems to be getting 3 total days from somewhere, which is strange as I currently don't have any days set but previously had 5.

Please help!


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


#2

Check the indentation on that line. It only returns when the elif branch takes effect.


#3

Thank you! That's all it was, such a simple mistake.


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.