Learn Python - take a vacation


#1

https://www.codecademy.com/courses/learn-python/lessons/taking-a-vacation/exercises/transportation-?action=resume_content_item

I have a very specific question about why one way to write the answer is incorrect while the other is not:
This would not let me pass on to the next part of the lesson:

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

But when I remove the return statements it likes the code more:

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

For all of the lessons thus far, the instructions have said to return or print after each if/elif/else statement. Why is that not true for this exercise?


#2

Correctness is defined by the lesson author. Most SCT’s are not exhaustive, and quite selective in fact. The second example is the one I would go with, though.


To achieve consistency in indentation, always check for mixed tabs and spaces, or differing indents at the same structural level (block). It looks like spaces on one, and a tab on the other. Clean that sort of stuff up in the write stage.


Extra Study

When you complete the Advanced Topics unit this will make perfect sense. Set it aside until then so you don’t disrupt your learning path.

>>> def rental_car_cost(days):
	return 40 * days - (days // 3 and 1) * 20 - (days // 7 and 1) * 30

>>> [rental_car_cost(x) for x in range(1, 15)]   # up to a fortnight
[40, 80, 100, 140, 180, 220, 230, 270, 310, 350, 390, 430, 470, 510]
>>> 

One cannot tell you that this sort of logic comes easily, or naturally to the average person; but, this is proof it is not out of reach.


#3

Hi @jgraddon5 ,

Both of your rental_car_cost function examples exemplify reasonable and common strategies for computing and returning a result. However, as @mtf states, the submission correctness test that the author of the exercise created might be particular regarding which, among many possible correct solutions, it will accept.

Note that both of your examples need some correction, although in the case of the second one, the problem might not be obvious, and may not even have caused the code to malfunction.

The first example is designed to compute and return a result in each conditional block, and includes a default return value in the final line of the function. Of course, for any given call of the function, only one of the return statements would execute, depending upon which condition is applicable, and that is fine.

However, the two return statements within the conditional blocks contain enhanced assignment operators, and that is an error. For example, this …

    return cost -= 50

… should be …

    return cost - 50

That is why the SCT would not accept the first submission.

The second example computes a result within whatever conditional block applies, and returns a value at the end, so all calls to the function eventually terminate when the final line is reached.

Another important issue is that in the first example, this statement utilizes two spaces followed by a tab for indentation …

  	return cost -= 20

… and the same is true of this statement in the second example …

  	cost -= 20

Using a mixture of tabs and spaces for indentation in a Python program should be avoided, and usually leads to problems, even if it did not do so in this case.