[Tutor] Why do I not get the same results for these two functions when I pass 7 as an argument?

Mark Lawrence breamoreboy at yahoo.co.uk
Fri May 3 09:07:46 CEST 2013


On 03/05/2013 07:10, Nonso Ibenegbu wrote:
> Hello everyone,
> Wonder if someone can help me understand why these two codes do not give
> the same results for what looks essentially the same ("?") code. The
> argument passed is
> 7.
>
> def rental_car_cost(days):
>      payment = days * 40
>      if days >= 7:
>          return payment - 50
>      elif days >= 3 < 7:
>          return payment - 20
>      else:
>          return payment
>
> and...
>
> def rental_car_cost(days):
>      payment = days * 40
>      if days >= 3 < 7:
>          return payment - 20
>      elif days >= 7:
>          return payment - 50
>      else:
>          return payment
>

Python comparisons are chained.  days >= 3 < 7 is saying "days is 
greater or equal to 3 and 3 is less than 7".  You need 3 <= days < 7.

-- 
If you're using GoogleCrap™ please read this 
http://wiki.python.org/moin/GoogleGroupsPython.

Mark Lawrence



More information about the Tutor mailing list