1

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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
ALLCAPS
  • 65
  • 1
  • 5
  • `if False` will never happen... Also, I think you want `cashPool += 300` to update the pool, not assign `result` at all – OneCricketeer Oct 29 '16 at 21:01

2 Answers2

0

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')
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • I can't believe I completely forgot += instead of just =. I swear 95% of the time I come across an issue when I try to program it's literally me forgetting one character or something so minor. Thank you though! – ALLCAPS Oct 29 '16 at 21:04
0

Firstly, random.randint(3,4) only returns 3 or 4... needs to be (0,4) if you want the rest of the values.

Secondly, this section won't work because if False isn't entered ever.

AskYesNo('Would you like to spin? Enter y/n ')
if False:
    break

You need to get the boolean value from that function.

if AskYesNo('Would you like to spin? Enter y/n '):
    break

Some other suggestions include not repeating yourself so much. Make it DRY

For example, borrowing from

Python: determine if all items of a list are the same item

def all_same(items, element):
    return all(x == element for x in items)

You can pull out some variable constants, use lists, and some functional programming to check your slots.

This, way, you just play with the numbers and you have a whole different game with minimal changes needed to be made.

total_slots = 3
max_range = 4
multiplier = 50

while True:
    i += 1
    spins = [random.randint(0, max_range) for _ in range(total_slots)]
    print('\t'.join(map(str, spins)))

    credits = 0
    for i in range(max_range + 1): # We loop over all spin possible values
        won = all_same(spins, i)
        if won:
            credits = (i + 1) * multiplier #  when i == 0, we add 50
            print('Congratulations! You have won {} credits'.format(credits))
            break #  can stop the for-loop

    cashPool += credits  #  if you didn't win, nothing is added
    print('You currently have {} credits'.format(cashPool))

    if AskYesNo('Would you like to spin? Enter y/n '):
        break

Note: I know I didn't include the wheel values. That all_same method will still work an soon as you get the strings for each spin value in the wheel lists.

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245