[Tutor] Running a loop

Robert Sjoblom robert.sjoblom at gmail.com
Sat Oct 15 22:07:41 CEST 2011


> Subject: [Tutor] Running a loop
> I am using Windows 7 and python 3.1. This is a block from a slot machine
> code. It takes the random generated words and indicates if it wins or loses.
> I can't figure out why it wants to print the last print statement 'Loser' no
> matter if the elif statements are true.

Let's begin with saying "never mind that, you have other issues".
First of all, whitespace is great in moderation. I don't know if it's
your mail client or your code that's messed up, but the kind of
whitespace you're sending along is just horrible and nigh on
unreadable. This leads to the second issue: you're misspelling strings
and comparing uppercase strings to lowercase ones (which will
automatically fail).

> import random
>
> wheel1=['ZOMBIE', 'WITCH', 'CAT', 'GHOST',
> 'CANDY','PUMPKIN','PUMPKIN','CANDY', 'GHOST','CANDY']
>
> wheel2=['ZOMBIE', 'WITCH', 'CAT', 'GHOST',
> 'CANDY','PUMPKIN','PUMPKIN','CANDY', 'GHOST','CANDY']
>
> wheel3=['ZOMBIE', 'WITCH', 'CAT', 'GHOST',
> 'CANDY','PUMPKIN','PUMPKIN','CANDY', 'GHOST','CANDY']

You don't really need 3 different wheels if they're all identical,
instead having one wheel is enough:
wheel = ["ZOMBIE", "WITCH", "CAT" #and so on]

> wheel1index=''
> wheel2index=''
> wheel3index=''

> #decide images for each slot
> wheel1index=wheel1[random.randint(0, len(wheel1) - 1)]
> wheel2index=wheel2[random.randint(0, len(wheel2) - 1)]
> wheel3index=wheel3[random.randint(0, len(wheel3) - 1)]

Here you can just do wheel1index = wheel[random.randint(o, len(wheel)
- 1)], then wheel2index = wheel[... and so on.

> print(wheel1index)
> print(wheel2index)
> print(wheel3index)

> #winning combonations
> def checkwin (wheel1index, wheel2index, wheel3index):
>    if wheel1index=='zombie' and wheel2index=='zombie' and wheel3index==
> 'zombie':
>        return print ('wins Zombies.')
I suspect that it's your mail client that makes it look horrible. You
can also do:
if wheel1index == wheel2index == wheel3index:
    return print("Wins {0}".format(wheel1index))
or something similar for all of the cases where each wheel shows the
same. However, you won't get any results unless you change the case to
match what you've actually assigned to the wheels (remember, we assign
"WITCH" and "CAT" but it checks for "witch" and "cat" and will never,
ever find them).


>    elif wheel1index=='ghost' and wheel2index=='pumpkin' and wheel3index==
> 'candie':
>
>        print ('wins pumpkin, ghost, and candy.')

Here you've misspelled "candy" as well as having it in the wrong case.
ANYWAY, I'm sure there's some better way to compare stuff like:
Pumpkin, Pumpkin, Ghost
Pumpkin, Ghost, Pumpkin
Ghost, Pumpkin, Pumpkin

(I mean, it's the SAME SITUATION, and the same goes for almost all the
rest of the elif structures), but I'm in a hurry so I can't really
come up with a good way to check for that. Possibly have one index (a
list with three "wheel" items) and do some cool list comprehensions on
it, as it is I'm 100% sure that your if-elif-else structure can be
optimized a lot.

Also, no need to have a new line between each elif, makes it hard to
read, and they're all part of the same code block.

-- 
best regards,
Robert S.


More information about the Tutor mailing list