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

Alan Gauld alan.gauld at btinternet.com
Fri May 3 20:10:55 CEST 2013


On 03/05/13 17:51, Nonso Ibenegbu wrote:

> The second function breaks down when the argument is 7 or above.
>
> Yet the only difference is that the condition "if days >= 7:" comes
> first in the first function but comes second (as "elif days >= 7:") in
> the second code.

Yes but that's because the other expression is nonsense (to python).

You cannot write

if days >= 3 < 7:

It doesn't make any sense.

What you mean is

if days >=3 and days < 7

If you fix that then the two functions will have similar logic.

In Python 9unlike most languages) you can abbreviate that but not the 
way you did it.
You need to use

if 3 <= days < 7:

Note that the variable is in the middle and the test for 3
is now reversed (3<=days). If in doubt use the double test
combined by 'and' as above.



-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list