I’m trying to make a slot machine. When testing if the jackpot worked, I found out that even if all three slots are equal to each other, it will only calculate the winnings as if there were only two matching slots.
if __name__ == '__main__':
import random
a = 'orange'
b = 'strawberry'
c = 'pineapple'
d = 'apple'
e = 'jackpot'
slots = [a, b, c, d, e]
winnings = 0
print('Play slot machine? (yes or no)')
play = input()
while play == 'yes':
print('Please enter a bet amount:')
bet = float(input())
slot1 = random.choice(slots)
slot2 = random.choice(slots)
slot3 = random.choice(slots)
print(slot1, " ", slot2, " ", slot3)
if slot1 == slot2:
winnings = bet*2
elif slot2 == slot3:
winnings = bet*2
elif slot1 == slot3:
winnings = bet*2
elif slot1 == slot2 == slot3:
winnings = bet*4
elif all(slot1 and slot2 and slot3) == e:
winnings = bet*10
else:
winnings = 0
print(winnings)
print('Play slot machine again? (yes or no):')
play = input()
Winnings should be 250 since all three slots are the jackpot, but winnings are 50
I tried making them all elifs, but as expected, that didn’t work.
>Solution :
elif is hurting you how you have this set up.
If, in the case where s1 == s2 == s3, the first if statement (s1 == s2), is executed, and no other elif block is further executed.
If you flip the order of your elif chain, you should get the result you expect.
import random
if __name__ == '__main__':
a = 'orange'
b = 'strawberry'
c = 'pineapple'
d = 'apple'
e = 'jackpot'
slots = [a, b, c, d, e]
winnings = 0
print('Play slot machine? (yes or no)')
play = input()
while play == 'yes':
print('Please enter a bet amount:')
bet = float(input())
slot1 = random.choice(slots)
slot2 = random.choice(slots)
slot3 = random.choice(slots)
print(slot1, " ", slot2, " ", slot3)
winnings = 0
if slot1 == slot2 == slot3:
if slot1 == e: # all are e
winnings = bet*10
else:
winnings = bet*4
elif slot1 == slot2:
winnings = bet*2
elif slot2 == slot3:
winnings = bet*2
elif slot1 == slot3:
winnings = bet*2
print(winnings)
print('Play slot machine again? (yes or no):')
play = input()