proper loop syntax?

JLowder at 360commerce.com JLowder at 360commerce.com
Mon Aug 11 12:04:02 EDT 2003


Standard disclaimer here, I'm new to Python/Jython..

I have what should be a really simple for loop with nested if/elif's which 
doesn't seem to function correctly. 

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

mask = "XXNNxxASSs"

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

 for y in mask:
     if y == "x":
         if t == 0:
           license_char = "5"
         elif t == 1:
           license_char = "P"
         elif t == 2:
           license_char = "o"
         elif t == 3:
           license_char = " "
         elif t == 4:
           license_char = "*"

     elif y == "a" or "S":
        if t == 0:
           license_char = "T"
        elif t == 1:
           license_char = " "
        elif t == 2:
           license_char = "w"
        elif t > 2:
           license_char = "t"

     elif y == "X" or "s":
        if t == 0:
           license_char = "7"
        elif t == 1:
           license_char = "G"
        elif t == 2:
           license_char = "f"
        elif t == 3:
           license_char = "*"
        elif t > 3:
           license_char = "u"

     license = license + license_char

The variable license ends up being TTTT55TTTT for the first pass.   The 
5's are correct, but the T's should only be there for two of the 
characters (I'm expecting the value to be "772255LTT7").   To save on the 
length of the email, I'm leaving out two more elif's.  If I were to move 
the elif y == "X" or "s": to be the first elif, the T's would then become 
7's.  The first elif is the only one being used.  What I don't understand 
is why is the first elif even being looked at if y does not equal a or S? 
My spacing works, because I endup with indent errors otherwise.  The 
structure looks correct for what I'm used to in other scripting 
languages...

What stupid human trick am I pulling here?

Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20030811/aac670ac/attachment.html>


More information about the Python-list mailing list