Help For Slot Machine Code - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Help For Slot Machine Code (/thread-25422.html) |
Help For Slot Machine Code - Rayaan - Mar-30-2020 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:
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 = TrueThanks! RE: Help For Slot Machine Code - SheeppOSU - Mar-30-2020 There are just a few slight changes that need to be made for this to work coins += reward_dict[match][number] on line 61and 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. |