proper loop syntax?

Skip Montanaro skip at pobox.com
Mon Aug 11 12:20:49 EDT 2003


    JLowder> The problem is, only the first IF/ELIF are read.  None of the
    JLowder> other elif's are used.  In fact, the first elif is used when it
    JLowder> shouldn't be and I don't know why.  This is my code snippet:

    JLowder> mask = "XXNNxxASSs"

    JLowder> for t in range(licenses):   # licenses = 5

    JLowder>  for y in mask:
    JLowder>      if y == "x":
                      ...

    JLowder>      elif y == "a" or "S":
                      ...

    JLowder>      elif y == "X" or "s":
                      ...

    JLowder>      license = license + license_char

The elif clauses are incorrect.  "X or "s" is valid, but doessn't do what
you think it is doing (try fiddling with it at an interpreter prompt).  You
want

         if y == "x":
             ...

         elif y == "a" or y == "S":
             ...

         elif y == "X" or y == "s":
             ...

or

         if y == "x":
             ...

         elif y in ["a", "S"]:
             ...

         elif y in ["X", "s"]:
             ...

Skip





More information about the Python-list mailing list