# proper loop syntax?

sismex01 at hebmex.com sismex01 at hebmex.com
Mon Aug 11 18:20:39 CEST 2003

```heh heh :-)

Congratulations on picking python, you'll have lotsa hours
of pure python programming penjoyment (ok, so I dont have
impeccable spelling) ;-)

Anyway.  What we have here, is the need of more data-oriented
coding, and not necessarily a lot of chained if-elif-elif...
statements.

> 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 y == "a" or "S":
>         if t == 0:
>            license_char = "T"
>         elif t == 1:
>            license_char = " "
....
>      elif y == "X" or "s":
>         if t == 0:
>            license_char = "7"
>         elif t == 1:
>            license_char = "G"
....

So, you´re only testing a few variables: "y" and "t",
and they have pre-known values in each case, and also,
it´s a simple translation.

You can create a dictionary-of-lists which will
do all of the decision's regarding the selection of
license_char without the if´s.  Something like:

MAIN = {}
MAIN["x"] = [ "5", "P", ... ]
MAIN["a"] = [ "T", " ", ... ]
MAIN["S"] = MAIN["a"]
MAIN["X"] = [ "7", "G", ... ]
MAIN["s"] = MAIN["X"]

So, in this "pseudo-matrix", all decisions are already
taken, there´s no need to be iffing around with fixed
values and translations; this is data-oriented programming.

Now your loop becomes:

for t in range(licenses):
for y in mask:

As to why if-elif-elif... doesn´t perform as we think it
does, I´ve never had to investigate it.  So much decisions
sometimes means that the algorith must be rethought into
a better one.

HTH!

-gustavo
Advertencia:La informacion contenida en este mensaje es confidencial y
restringida, por lo tanto esta destinada unicamente para el uso de la
persona arriba indicada, se le notifica que esta prohibida la difusion de
este mensaje. Si ha recibido este mensaje por error, o si hay problemas en
la transmision, favor de comunicarse con el remitente. Gracias.

```