Oops, try again. Did you create a function called rental_car_cost?


#1

Below is my code which seems right… I am not sure why I am getting the error : Oops, try again. Did you create a function called rental_car_cost?

If someone could check and help plzzzzzzz

def hotel_cost(nights):
return 140 * nights
hotel_cost(3)
def plane_ride_cost(city):
if city == “Charlotte”:
return 183
elif city == “Tampa”:
return 220
elif city == “Pittsburg”:
return 222
elif city == “Los Angeles”:
return 475
plane_ride_cost(Pittsburg)
def rental_car_cost(days):
cost = 40*days
if days >= 7:
return cost - 50
elif days >= 3:
return cost - 20
else:
return cost
rental_car_cost(8)


#2

Let’s start with plane_ride_cost(Pittsburg) on line 13 or 14 first. Pittsburg is a string. We denote strings by wrapping the characters with single or double quotes like this: “Pittsburg”. Therefore, that would change the code to this: plane_ride_cost("Pittsburg").

For the rental_car_cost function, make sure that all of the code is indented like this:

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

Python uses the indentation to denote the code block after the colons. Each level matters.

  1. The code indented under def rental_car_cost(days): belongs to that function.
  2. The code under the if days >= 7: belongs to that if code block.
    and so on…

Why? The code under the function definition only run when that function is called (or invoked). The code under the if only runs IF the conditional expression is true. The code only the elif only runs IF that conditional expression is true. The code under the else only runs if the other if and elif did not run.

The indentations indicate what controls those code blocks and if they’ll run.


#3

Yes, it was a indentation issue.
Fixed.

Thanks :slight_smile:


#4

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