Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help For Slot Machine Code
#1
I am trying to make a slot machine but am getting some errors that I cannot fix. It is giving me the wrong amount of coins for each win and a single dollar will not give me any coins (Look at rules below for clarification). I am not sure of any other issues that I could be missing but I believe that it is also only allowing you to win once. Any help is appreciated!

The rules for the slot machine are:
  • One player game starts with 100 coins.
    One turn costs 5 coins.
    Keep playing until you have no money left, or the player chooses to 'cash out'.
    Each go randomly spins the three spinners. Each spinner has 5 symbols: Bell, Cherry, Banana, Dollar, Skull.
    3 of any fruit = 50 coins
    2 of the same fruit = 10 coins
    3 Bells = 1000 coins
    2 Bells = 100 coins
    3 Dollar = 500 coins
    2 Dollar = 50 coins
    1 Dollar = 1 coin
    If any skull appears on any spinner you may not win any money.
    Some examples:
    Cherry Cherry Skull = 0 coins
    Banana Cherry Banana = 10 coins
    Cherry Banana Dollar = 1 coin
    Skull Dollar Dollar = 0 coins
    Banana Banana Dollar = 11 coins (10 + 1)

Here is the code:


import tkinter as tk
import random
#I added an extra item
#Check line 37
  
print("Welcome to the Virtual Slot Machine")
global coins
coins = 100
symbols = ["bells", "cherries", "bananas", "dollars", "skull"]
done = False
  
def sort(lists):
  for i in range(len(lists)-1):
    for x in range(len(lists)-1-i):
      if lists[x] < lists[x + 1]:
        lists[x], lists[x + 1] = lists[x + 1], lists[x]
  
  return lists
  
def open_file():
  f = open("highscores.txt","r")
  line = f.readlines()
  f.close()
  list1 = [int(i) for i in line[0].split(",")]
  ordered = sort(list1)
  return ordered
    
print("The highscores so far from higherst to smallest are: ")
print(*open_file())
 
def Match(slot1, slot2, slot3):
  if slot1 == slot2 and slot2 == slot3:
    return slot1, 1
  elif slot1 == slot2:
    return slot1, 0
  elif slot2 == slot3:
    return slot2, 0
  elif slot1 == slot3:
    return slot3, 0
  return None, None
  
def turn(coins):
  coins -= 5
  fruits = ["cherries", "bananas"]
  slot1 = random.choice(symbols)
  slot2 = random.choice(symbols)
  slot3 = random.choice(symbols)
  slots = [slot1, slot2, slot3]
  reward_dict = {"dollars" : [50, 500], "bells": [100, 1000]}
  for fruit in fruits:
      reward_dict.update({fruit: [10, 50]})

  if random.randint(0,10000) == 1:
    slot3 = "Gold Bar"
 
  print("You got {} {} {}".format(slot1,slot2,slot3))
  
  if "skull" not in slots:
    match, number = Match(slot1, slot2, slot3)
    if match != None:
      coins += reward_dict[match][number-1]
      print("You got {} {}! +{} coins!".format(number+2, match, reward_dict[match][number]))
 
  else:
    print("Unlucky, you got a skull, you lose!")

  if slot1 == "dollar" or slot2 == "dollar" or slot3 == "dollar":
    coins += 1

  if slot3 == "Gold Bar":
    print("Jackpot! All coins times 10!")
    coins = coins*10
  return coins
  
while coins > 0 and done == False:
  print("You have {0} coins".format(coins))
  play = input("Do you want to play a round? It costs 5 coins? y/n ")
  coins_left = coins - 5
  if play.lower == "y" and coins_left > 0:
    coins = turn(coins)
  else:
    if coins_left < 0:
      print("Sorry, you do not havwe enough money")
      print("You ended with {0} coins".format(coins))
    else:
      print("Ok, thanks for playing")
      print("You ended with {0} coins!".format(coins))
      done = True
Thanks!
Reply
#2
There are just a few slight changes that need to be made for this to work
coins += reward_dict[match][number] on line 61
and then a little code snippet on line 63
else: #Else statement for the "if match != None:"
    if "dollar" in slots: #I put the if inside the else, in case you plan on adding any more symbols that reward money upon only having one symbol. However, you can also incorporate a dictionary for this to organize the code.
        coins += 1
        print("You got dollar! +1 coin!")
Then get rid of the code on line 67 and 68
Also move the code on lines 70-73 into the if "skull" not in slots: statement.
I have not tested it so hopefully that fixes it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Gui slot machine-out of memory error steve_shambles 12 4,988 May-18-2020, 11:31 PM
Last Post: deanhystad
  run into issues while configuring visual-studio code on a win 7 machine. apollo 3 3,614 Sep-12-2019, 12:25 AM
Last Post: snippsat
  Sending Hex Code to Machine from Laptop using TCP/IP Protocal Brian_Cheong 2 9,622 May-12-2017, 01:01 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020