Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Variable not updating
#1
I am making a slot machine and I am not very experienced at coding. I am struggling to get the coins variable to update at all when I you either gain or lose any. I am unsure of whether it is just the output of it or the actual variable. Here is my 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 = ["bell", "cherry", "banana", "dollar", "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 turn(coins):
  coins -= 5
  fruit = ["cherry", "banana"]
  slot1 = random.choice(symbols)
  slot2 = random.choice(symbols)
  slot3 = random.choice(symbols)
  if random.randint(0,10000) == 1:
    slot3 = "Gold Bar"
  slots = [slot1,slot2,slot3]
  print("You got {} {} {}".format(slot1,slot2,slot3))

  if "skull" not in slots:
    if (slot1 and slot2 and slot3) == "dollar":
      print("You got 3 dollars! +500 coins")
      coins += 500
    elif (slot1 and slot2) or (slot1 and slot3) or (slot2 and slot3) == "dollar":
      print("You got 2 dollars! +50 coins")
      coins += 50
    elif "dollar" in slots:
      print("You got 1 dollar! + 1 coin")
      coins += 1
    if (slot1 and slot2 and slot3) in fruit:
      print("You got 3 fruit! +50 coins!")
      coins += 50
    elif ((slot1 and slot2) or (slot1 and slot3) or (slot2 and slot3) in fruit) and (slot1 == slot2) or (slot1 == slot3) or (slot2 == slot3):
      print("You got 2 of the same fruit! +10 coins")
    if (slot1 and slot2 and slot3) == "bell":
      print("You got 3 bells! +1000 coins!")
      coins += 1000
    elif (slot1 and slot2) or (slot1 and slot3) or (slot2 and slot3) == "bell":
      print("You got 2 bells! +100 coins")
  else:
    print("Unlucky, you got a skull, you lose!")
  if slot3 == "Gold Bar":
    print("Jackpot! All coins times 10!")
    coins = coins*10

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 == "y" and coins_left > 0:
    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
Reply
#2
When you first initiate the variable coins, it is a public variable so to say across the entire program, except in classes and functions. The function, when executed, creates its own private coins variable. Now you can use Global Variables, however, many people do not recommend that. Instead, use the return function like so. (Check lines 67 and 74).
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 = ["bell", "cherry", "banana", "dollar", "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 turn(coins):
  coins -= 5
  fruit = ["cherry", "banana"]
  slot1 = random.choice(symbols)
  slot2 = random.choice(symbols)
  slot3 = random.choice(symbols)
  if random.randint(0,10000) == 1:
    slot3 = "Gold Bar"
  slots = [slot1,slot2,slot3]
  print("You got {} {} {}".format(slot1,slot2,slot3))
 
  if "skull" not in slots:
    if (slot1 and slot2 and slot3) == "dollar":
      print("You got 3 dollars! +500 coins")
      coins += 500
    elif (slot1 and slot2) or (slot1 and slot3) or (slot2 and slot3) == "dollar":
      print("You got 2 dollars! +50 coins")
      coins += 50
    elif "dollar" in slots:
      print("You got 1 dollar! + 1 coin")
      coins += 1
    if (slot1 and slot2 and slot3) in fruit:
      print("You got 3 fruit! +50 coins!")
      coins += 50
    elif ((slot1 and slot2) or (slot1 and slot3) or (slot2 and slot3) in fruit) and (slot1 == slot2) or (slot1 == slot3) or (slot2 == slot3):
      print("You got 2 of the same fruit! +10 coins")
    if (slot1 and slot2 and slot3) == "bell":
      print("You got 3 bells! +1000 coins!")
      coins += 1000
    elif (slot1 and slot2) or (slot1 and slot3) or (slot2 and slot3) == "bell":
      print("You got 2 bells! +100 coins")
  else:
    print("Unlucky, you got a skull, you lose!")
  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 == "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

I also would like to point out that your code is a bit messy. Dictionaries can often be very useful in neatening code and shortening it. I applied this to your main turn function. If you learn to work with dictionaries like this it will be very useful in the future. I'd suggest that once you finish it, you clean it up a bit with this. You can also use the time.sleep from the time library import time and use it in between certain things so not everything is just printed at once.
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
  if slot1 == slot2:
    return slot1, 0
  if slot2 == slot3:
    return slot2, 0
  if 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]
  RewardDict = {"dollars" : [50, 500], "bells": {100, 1000}}
  for fruit in fruits:
      RewardDict.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 += RewardDict[match][number-1]
      print("You got {} {}! +{} coins!".format(number+2, match, RewardDict[match][number]))

  else:
    print("Unlucky, you got a skull, you lose!")
    
  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 == "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
Reply
#3
(Mar-29-2020, 01:33 AM)SheeppOSU Wrote: if "skull" not in slots:
match, number = Match(slot1, slot2, slot3)

Sorry if this is a simple question but what does the comma do between match and number?

(Mar-29-2020, 01:33 AM)SheeppOSU Wrote: def Match(slot1, slot2, slot3):
if slot1 == slot2 and slot2 == slot3:
return slot1, 1
if slot1 == slot2:
return slot1, 0
if slot2 == slot3:
return slot2, 0
if slot1 == slot3:
return slot3, 0
return None, None

Also shouldn't all the ifs other than the first be elifs so it doesn't have multiple outputs?

Thanks for the help, I going to try to use dictionaries more

(Mar-29-2020, 01:33 AM)SheeppOSU Wrote: def Match(slot1, slot2, slot3):
if slot1 == slot2 and slot2 == slot3:
return slot1, 1
if slot1 == slot2:
return slot1, 0
if slot2 == slot3:
return slot2, 0
if slot1 == slot3:
return slot3, 0
return None, None

Sorry, I was just reading through and I realised I do not know how this works. If you could explain that would be very helpful thanks. I just don't know how returning None, None gives anything or what it does.
Reply
#4
(Mar-29-2020, 12:51 PM)Rayaan Wrote: Sorry, I was just reading through and I realised I do not know how this works. If you could explain that would be very helpful thanks. I just don't know how returning None, None gives anything or what it does.
First off, since it returns the value, the function ends, and it won't run through the if statements after it, so no need for an elif. On the first if, it check to see if all the slots are equal, if so then it returns what they matched in and an index number to use with the RewardDict. Separating the values with commas sends two different values which are retrieved the same way that they are returned from the function. The next three if statements check to see if any of the two slots match, it was already determined after the first if, that they don't all three match, so if two of them match, then it returns the match and an index number to access the correct reward from RewardDict. At the very end it return None, None if there are no matches. Hopefully this answers your question. If you'd like me to clarify on something I'd be happy to do so.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variable Not Updating NectDz 1 2,799 Jun-07-2020, 11:21 PM
Last Post: bowlofred
  Global Variable Not Updating joew 2 7,834 Jan-26-2020, 04:15 PM
Last Post: joew

Forum Jump:

User Panel Messages

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