[Tutor] [Tutor} Why doesn't it choose a new number each time?
rikard.bosnjakovic at gmail.com
Thu Feb 15 10:06:40 CET 2007
On 2/15/07, Nathan Pinno <falcon3166 at hotmail.com> wrote:
> and that was a suggestion. (Thanks!) But can anyone explain why I can
> shorten the code? I looked at it a long while yesterday, and came up with
> nothing. The only thing I decided was to try to get it to the GUI stage.
If you take a look at your if-cases, you see that a lot of strings in
there are the same. The code is therefore redundant, and this is what
can be trimmed down.
Consider this code:
if var1 = 1 and var2 = 0:
print "Var1 is 1, var2 is 0"
if var1 = 0 and var2 = 0:
print "Var1 is 0, var2 is 0"
if var1 = 1 and var2 = 1:
print "Var1 is 1, var2 is 1"
if var1 = 0 and var2 = 1:
print "Var1 is 0, var2 is 1"
This scope is redundant in a lot of ways and can be trimmed down to
one if and one print:
if (var1 in [0,1]) and (var2 in [0,1]):
print "Var1 is %d, var2 is %d" % (var1, var2)
Your code is similiar to the above. It takes time to learn how to trim
down redundant code. Vast knowledge of a language itself is not
required, but a pretty solid knowledge about the basics is probably
required. If you are entirely new to programming, it might be
Give it a try, and don't hesitate to ask again.
More information about the Tutor