ALLCAPS ALLCAPS - 1 month ago 5
Python Question

Adding result of a slot machine roll in loop

I may have a hard time explaining my issue, so bare with me. I am trying to create a program that works like a slot machine. In my loop, I am trying to determine how many credits a player gets based on the result of their spin(s). However, say when a player rolls and wins 300 credits, when asked to spin again, if they win another time, it doesn't remember the result of the former spin.

I know this is a simple fix, but basically each time the loop goes back again it forgets their updated total. I am new to programming, and this stuff is confusing as all hell to me.

Here's what I have for the loop section:

cashPool = 500

import random

# Assigning wheel properties
wheel1 = ['Candy','Ghost','Pumpkin','Cat','Zombie','Witch','Witch','Cat','Ghost','Candy']
wheel2 = ['Candy','Ghost','Pumpkin','Cat','Zombie','Witch','Cat','Pumpkin','Ghost','Candy']
wheel3 = ['Candy','Ghost','Pumpkin','Cat','Zombie','Witch','Pumpkin','Candy','Candy','Ghost']

#loop to spin


def AskYesNo(question):
tmp = input(question).lower()
if tmp.startswith('y'):
return True
else:
return False

i = 0

while True:
spin1 = random.randint(3,4)
spin2 = random.randint(3,4)
spin3 = random.randint(3,4)
print(str(wheel1[spin1])+ '\t' + str(wheel2[spin2]+ '\t' + str(wheel3[spin3])))
i += 1
if spin1==4 and spin2==4 and spin3==4:
print('Congratulations! You have won 300 credits')
result = cashPool + 300
print('You currently have '+ str(result) +' credits')
if spin1==5 and spin2==5 and spin3==5:
print('Congratulations! You have won 250 credits')
result = cashPool + 250
print('You currently have '+ str(result)+ ' credits')
if spin1==3 and spin2==3 and spin3==3:
print('Congratulations! You have won 200 credits')
result = cashPool + 200
print('You currently have '+cashPool+' credits')
if spin1==2 and spin2==2 and spin3==2:
print('Congratulations! You have won 150 credits')
result = cashPool + 150
print('You currently have '+cashPool+' credits')
if spin1==1 and spin2==1 and spin3==1:
print('Congratulations! You have won 100 credits')
result = cashPool + 100
print('You currently have '+cashPool+' credits')
if spin1==0 and spin2==0 and spin3==0:
print('Congratulations! You have won 50 credits')
result = cashPool + 50
print('You currently have '+cashPool+' credits')
AskYesNo('Would you like to spin? Enter y/n ')
if False:
break


Also, I'm 100% sure there is an easier way to calculate the results of each spin rather than doing if statements over and over. But again, I'm new, and that's beyond me. I can't think like a programmer, I don't know how people do it.

Answer

Instead of defining a new result variable, just increment the existing cashPool variable you have. E.g.:

if spin1==4 and spin2==4 and spin3==4:
    print('Congratulations! You have won 300 credits')
    cashPool += 300 # Here
    print('You currently have '+ str(result) +' credits')
Comments