I cant add or subtract a variable

Question:
so I can’t work the -= in Python
Repl link:
https://replit.com/@dft-fritz/slots#main.py

if money > 0 :
      money -= 5 
      roll1()
      roll2()
      roll3()
      checkwin()

@9pfs1 can you help with this one?

Could you fix the link formatting?

what do you mean fix it

[link name](actual link)

I’m not a Python expert, but I believe you ran the money -= 5 in an if statement, this will probably not affect the money variable after the if statement is finished.

1 Like
if money > 0 :
      money -= 5 
      roll1()
      roll2()
      roll3()
      checkwin()

can you share the error lol

the money doesn’t subtract 5 after rolling the slots

ok so you have a logic error?

i think i might have that

ok but can you tell me how you know it doesn’t subtract?

ok so when you roll the slots it is supposed to subtract 5 dollars for payment but it doesn’t

1 Like
#imports 
import time, os ,random 
from colored import fg 

#variable set-up
color = fg('white')
color1 = fg('red')
color2 = fg('yellow')
color3 = fg('green')
color4 = fg('blue')
slot = 0
slot1 = 0
slot2 = 0
slot3 = 0
money = 20


#lists
roll = []

#subroutines 
def roll1(): 
  global slot1
  slot1 = random.randint(1,10) 
  if slot1 == 1 or  slot1 == 6: 
    roll.append("😀") 
  elif slot1 == 2 or slot1 == 7:
    roll.append("😁") 
  elif slot1 == 3 or slot1 == 8:
    roll.append("😭") 
  elif slot1 == 4 or slot1 == 9:
    roll.append("🥳") 
  elif slot1 == 5 or slot1 == 10:
    roll.append("😴")

def roll2(): 
  global slot2
  slot2 = random.randint(1,10) 
  if slot2 == 1 or  slot2 == 6: 
    roll.append("😀") 
  elif slot2 == 2 or slot2 == 7:
    roll.append("😁") 
  elif slot2 == 3 or slot2 == 8:
    roll.append("😭") 
  elif slot2 == 4 or slot2 == 9:
    roll.append("🥳") 
  elif slot2 == 5 or slot2 == 10:
    roll.append("😴")

def roll3():
  global slot3
  slot3 = random.randint(1,10) 
  if slot3 == 1 or  slot3 == 6: 
    roll.append("😀") 
  elif slot3 == 2 or slot3 == 7:
    roll.append("😁") 
  elif slot3 == 3 or slot3 == 8:
    roll.append("😭") 
  elif slot3 == 4 or slot3 == 9:
    roll.append("🥳") 
  elif slot3 == 5 or slot3 == 10:
    roll.append("😴")

def checkwin():
  global money
  if slot1 == slot2:
    money += 5 
  elif slot1 == slot3:
    money += 5
  elif slot2 == slot3:
    money += 5 

#main code 
while True: 
    
  print(color4 + "welcome to slots land what would you like to do?" + color)
  print("\n1: play slots \n2: upgrade slots \n3: check balance \n4: login \n"+str(money))
  menu = input("\n>")
  if menu == "1":
    os.system("clear")
    if money > 0 :
      money = money - 5
      roll1()
      roll2()
      roll3()
      checkwin()      
      for i in roll:
        print(i) 
        time.sleep(1) 
      roll.clear()
  elif menu == "2": 
    if money == 100:
      slot += 1  
  elif menu == "3": 
    print("Balance: " + str(money)) 
    time.sleep(2)
  elif menu == "4":
    password = os.environ['login'] 
    trypass = input("password?: ") 
    if trypass == password: 
      money += 200 
  os.system("clear")

  

the following code works because previously, if a function is called and it tries to edit a value that’s not defined in the function, it’s not affected outside. So using global var_name will cause it to be edited “globally”

1 Like

yes and i have a global in there but it wont work

1 Like

no I edited it :man_facepalming: remember you slots need to be edited by the roll functions or else they will always be equal to 0 ;-;

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.