[Tutor] if...elif question

alan.gauld@bt.com alan.gauld@bt.com
Wed, 3 Oct 2001 22:14:35 +0100


> ## a function to convert percentages to letter grades
> def grades(percent):
>     if percent < 60:
>         return 'F'
>     elif percent >= 60 < 70:

To Python this looks like:

      else:
          if percent >= (60 < 70):

which looks like in your case
          if 75 >= 1    # 1 => true

To get what you want you need two tests:

      elif (percent >= 60) and (percent < 70):

You may not need the parens but they help to make it obvious to 
both the reader and to python exactly what you want 'and'ed...

Its all to do with a thing called operator precedence which is 
explained on my (now restored) web tutor in the Simple Sequences topic.
And in full detail in the Python documentation.

HTH,

Alan G.
http://www.crosswinds.net/~agauld/