When is it too much or too little?



I know that there are many ways to to write a code and get the same result.
However I just want some clarification on the following-

I typed -

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

I noticed that other beginners typed -

elif days >= 3 and <= 6

Now both codes work.


Seeing that we already added 7 days or more in the "if", do we have to add it to the elif? I thought that the "if" takes precedence (7 or more), then "elif"(3 or more, but obviously not more or equal to 7 because "if" would have executed that) and then else ( neither if or elif was true).

Seeing that both codes work, which is 'more' correct or proper or 'this is what is expected when you do python coding'?

Thanks, hope what I ask makes sense.


Hmm....I did it like yours. I find adding the <=6 redundant. What if days happened to be 6.5 (6 and a half days)? They basically ignore it if it's ```elif days >=3 and <=6```.

Stick with elif days >= 3 it's enough to get you through without typing something extra.

This is my two cents for this topic. Someone else may have a different opinion/preference.


We can simplify this to reduce repetition:

cost = days * 40
if days >= 7:
    cost -= 50
elif days >= 3:
    cost -= 20
return cost


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