0

I'm trying to get rid of a error in python but so far every method i've tried hasn't worked. Any help would be greatly appreciated.

The code:

from sense_hat import SenseHat
import random
from guizero import App, PushButton,Text
import time
import pygame.mixer
from pygame.mixer import Sound
pygame.mixer.init()
sense = SenseHat()
app = App(title="Fruity Fruit Machine", bgcolor="yellow")
mario = Sound("SuperMarioBros.ogg")
money = 30.00
casino = 100.00
global money
global casino
def machine():
     sense.clear(0,0,0)
     images = ["apple.png", "orange.png", "lemon.png"]
     fruit = []
     money = money - 1.00
     for i in range(3):
          img = random.choice(images)
          fruit.append(img)
          sense.load_image(img)
          time.sleep(3)
          sense.clear(0,0,0)
     if all_same(fruit) == True:
          sense.show_message("You won £20!")
          casino = casino - 20.00
          money = money + 20.00
          display_casino()
          dispaly_money()
          dc = "Casino money: " + casino
          cm.set(dc)
          md = "Your money: " + money
          m.set(md)
     else:
          sense.show_message("You got 30p!")
          money = money + 00.30
          casino = casino + 00.70
          display_casino()
          display_money()
          dc = "Casino money: " + casino
          cm.set(dc)
          md = "Your money: " + money
          m.set(md)

def play_mario():
     mario.play()
def all_same(items):
     return all(x == items[0] for x in items)


button = PushButton(app, command=machine, text="SPIN")
button2 = PushButton(app, command=play_mario, text="Play Music")
cm = Text(app, text="",align="left")
m = Text(app, text="",align="right")

The error is: UnboundLocalError: local variable 'money' referenced before assignment. Python also sometimes comes up with a syntax warning: SyntaxWarning: name 'casino' is assigned to before global declaration

Oscar Peace
  • 99
  • 1
  • 2
  • 11

2 Answers2

0

You need to put global money and global casino within machine. Putting it outside a function does nothing. For an explanation of why, take a look at this answer.

ollien
  • 4,418
  • 9
  • 35
  • 58
0

Try moving the global declarations inside your function:

from sense_hat import SenseHat
import random
from guizero import App, PushButton,Text
import time
import pygame.mixer
from pygame.mixer import Sound
pygame.mixer.init()
sense = SenseHat()
app = App(title="Fruity Fruit Machine", bgcolor="yellow")
mario = Sound("SuperMarioBros.ogg")
money = 30.00
casino = 100.00
def machine():
    global money
    global casino
    sense.clear(0,0,0)
    images = ["apple.png", "orange.png", "lemon.png"]
    fruit = []
    money = money - 1.00
    for i in range(3):
        img = random.choice(images)
        fruit.append(img)
        sense.load_image(img)
        time.sleep(3)
        sense.clear(0,0,0)
    if all_same(fruit) == True:
        sense.show_message("You won £20!")
        casino = casino - 20.00
        money = money + 20.00
        display_casino()
        dispaly_money()
        dc = "Casino money: " + casino
        cm.set(dc)
        md = "Your money: " + money
        m.set(md)
    else:
        sense.show_message("You got 30p!")
        money = money + 00.30
Idanmel
  • 457
  • 4
  • 12