Python 2.4 | 7.3 The for statement

brainsucker jrodrigog at gmail.com
Wed Mar 23 00:05:45 EST 2005


>Still, this can be acomplished with the break statement, in a more
>clear way, with less variables (which implies less work for the gc and
>everybody).

Glad to read from you Francisco. :) Keep up that hard work, thanks.

I have been talking with those Python programmers (And Role players),
:) and I have made a bit of code for you avoiding the "and" keyword:

>>>cordure = 54
>>>monster = [ "Tchulu", "Golum", "Cerberus"]
>>>for wow in monster until cordure < 0:
>>> if wow == "Tchulu": cordure -= 30
>>> elif wow == "Cerberus": cordure -= 20
>>> elif wow == "Golum": cordure -= 5
>>>if cordure <= 0:
>>> print "End game"
>>>else:
>>> print "Next task"


This is your version:

>>>cordure = 54
>>>monster = [ "Tchulu", "Golum", "Cerberus"]
>>>for wow in monster:
>>> if wow == "Tchulu": cordure -= 30
>>> elif wow == "Cerberus": cordure -= 20
>>> elif wow == "Golum": cordure -= 5
>>> if cordure < 0: break # :|
>>>if cordure <= 0:
>>> print "End game"
>>>else:
>>> print "Next task"


a) As you can see, one more line of code...
Plus, in your code I do not see beforehand
when this roleplayer runs out of cordure.
# Little things matter. :)


b) Given that your IF condition is evaluated
ON every iteration, I see one line less on code,
evaluating the SAME conditions, for every
iteration.


c) I did not want to say this :|

+Debugging and friends:
+Tools > Search
+Break

ummmm in the new for I can see what is going
to happend beforehand, so I can search faster
when debugging:

"...our intellectual powers are rather
geared to master static relations..." [1]


d) I just wont more power on the just impressive
Python for loop, we can have it all.


[1] Go To Statement Considered Harmful
Edsger W. Dijkstra
http://www.acm.org/classics/oct95/



PD1: Francisco we have talked about this on the IRC,
the code presented is somehow TOY code to express
what I (Somebody?) want Python to do.

PD2: Try to imagine that nested loop with those
200+ lines in it ( Probably is bad coded :?, or not! )
and try to debug all those BREAKS.




More information about the Python-list mailing list