[Tutor] if...elif question
Andrei Kulakov
ak@silmarill.org
Tue, 02 Oct 2001 21:23:08 -0400
On Tue, Oct 02, 2001 at 09:17:23PM -0500, Chris Keelan wrote:
> I know the answer is staring me right in the face, but why does this little
> code snippet:
>
> ## a function to convert percentages to letter grades
> def grades(percent):
>
> if percent < 60:
> return 'F'
>
> elif percent >= 60 < 70:
elif 60 <= percent < 70:
> return 'D'
>
> else:
> return 'A'
>
> ##test the function
>
> percent=75
> mark=grades(60)
>
> print "%s percent is a grade of %s" % (percent, mark)
>
> ...produce this output:
>
> >> 75 percent is a grade of D
>
> ?!?
>
> Shouldn't the value of 75 fail the second elif test and return the 'else'
> value?
What happens is first percent is compared to 60, and the result is true.
then I think 60 is compared to 70, and that is always true.
Just a guess, and I didn't test my code but I'm almost certain it should
work fine.
>
> What am I doing wrong?
>
> - Chris
>
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
--
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org