proper loop syntax?

Bob Gailer bgailer at alum.rpi.edu
Mon Aug 11 12:48:19 EDT 2003


At 11:04 AM 8/11/2003 -0500, JLowder at 360commerce.com wrote:


>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":

y =="a" or "S" evaluates to "a" if y == "a" and evaluates to "S" otherwise.
Therefore the if condition is always true, and the code block following is 
executed.
The program never gets to elif y == "X" or "s":
Code it this way instead: elif y == "a" or  y == "S" or even better: elif y 
in "aS":

>         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?

I don't see a case for 'N', so m,y code below ignores it.

A better overall approach (IMHO) is to use dictionaries and lists:
char_sets = ['5Po *', 'T Wt', '7Gf*u']
mask_chars = {'x' : char_sets[0], 'a' : char_sets[1], 'S' : char_sets[1], 
'X' : char_sets[2], 's' : char_sets[2]}
license = ''
licenses = 5
mask = "XXNNxxASSs"
for t in range(licenses):
     for y in mask:
         if y in mask_chars:
             chars = mask_chars[y]
             if t < len(chars):
                 try:license += chars[t]
                 except:print y,t
print license


>Jason
>---
>Incoming mail is certified Virus Free.
>Checked by AVG anti-virus system (http://www.grisoft.com).
>Version: 6.0.506 / Virus Database: 303 - Release Date: 8/1/2003

Bob Gailer
bgailer at alum.rpi.edu
303 442 2625

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

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.506 / Virus Database: 303 - Release Date: 8/1/2003


More information about the Python-list mailing list