FAQ: Taking a Vacation - Transportation

This community-built FAQ covers the "Transportation " exercise from the lesson “Taking a Vacation”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Python

FAQs on the exercise _Transportation _

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

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

why this code don’t have return in if and elif function and also this code don’t have else why

1 Like

we manipulate the cost variable, then at the end function we return the cost.

in this case, we don’t need else, given we always want to return cost at the end of the function

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

taking_a_vacation_mycode
This is my code, but i get an alert?
What did i miss?

2 Likes

difficult to say, it seems you nested the rental_car_cost so its not available in the global namespace of the module.

4 Likes

Thank you very much. You are right. It was an indentation mistake. Just deleted the space bevore def rental_car_cost(days).

3 Likes

your editor have a problem.

File "python", line 17
    return x -= 50
              ^
SyntaxError: invalid syntax

This error is thrown by the python interpreter, got nothing to do with the editor.

you do too many operations at once here, you try to re-assign a variable and return the re-assigned variable.

Why doesn’t this work?

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

what if the car is rented for two days? That seems to be the most obvious flaw

what error message do you get?

1 Like

Why it is not working?
def rental_car_cost(days):

cost = 40 * days

if days >= 7:

return cost =- 50

elif 7 > days >= 3:

return cost =-20 

image

Here is the code I wrote, but not working, said something wrong with line16
image

what is =-? Its assigning a value of minus 50 to the return value. You can’t assign to return value.

It should be -=, thanks!

If you want to update the variable, yes. But is that really what you want to do?

You did not

return cost on line 20 after ‘cost -= 20’

Also, beware that return must be aligned with the if and elif for it to work properly.

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

how come it doesn’t need the “and days < 7” part to not get both discounts?

Because the first if statement already checked whether the days were 7 or more. If…elif…else statements evaluate in order, so for the function to reach the elif statement, it assumes that days are fewer than 7.

when using if, elif and else, once one of the conditions is met, the remaining conditions are skipped.

so in this case, lets say days = 9, then if is true, so the elif condition never runs. Which is why you don’t need to check that days is less then 7 in elif condition.