HELP! i dn know how to fix error

Hi. I making a mini-game and have a UnboundLocalError: local variable referenced before assignment. I dont know how to fix it.

----problem is here: ----
if operation == “Баланс”:

        self.formula = "У вас целых " + str(bal) + " Биткоинов!"

----and here----

elif operation == “Ремонт”:

        if k == True:

           self.formula = 'Ремонт не требуется!'

        elif k == False:

            if int(bal) >= 30:

                print('''Оборудование отремонтировано!

                с вас снято 30btc''')

                (bal) = bal - 30

there’s code:

from tkinter import *
lbl = 0

d = 0

k = True

tkb = ‘1’

items = ‘–’

bal = 0

class Main(Frame):

def __init__(self, root):

    super(Main, self).__init__(root)

    self.build()



def build(self):

    self.formula = "Меню"

    self.lbl = Label(text=self.formula, font=("callibri", 21, "bold"), bg="#000", foreground="#FFF")

    self.lbl.place(x=11, y=50)



    btns = [

        "Баланс", "Предметы", "Ремонт",

                   "Респект"

    ]



    x = 10

    y = 140

    for bt in btns:

        com = lambda x=bt: self.logicalc(x)

        Button(text=bt, bg="#FFF",

               font=("Comic sans", 15),

               command=com).place(x=x, y=y,

                                  width=115,

                                  height=79)

        x += 117

        if x > 400:

            x = 10

            y += 81



def logicalc(self, operation):

    if operation == "Баланс":

        self.formula = "У вас целых " + str(bal) + " Биткоинов!"

    elif operation == "Предметы":

        self.formula = 'Ваши предметы: '+ items

    elif operation == "Ремонт":

        if k == True:

           self.formula = 'Ремонт не требуется!'

        elif k == False:

            if int(bal) >= 30:

                print('''Оборудование отремонтировано!

                с вас снято 30btc''')

                (bal) = bal - 30

            else:

                print('''Недостаточно денег!

                ''')

    elif operation == "Респект":

        if d == 0:

         self.formula = ('''Доска почёта пока пуста.

Попадите туда, Поддержав нас!

         ''')

       

    else:

        if self.formula == "0":

            self.formula = ""

        self.formula += operation

    self.update()



def update(self):

    if self.formula == "":

        self.formula = "0"

    self.lbl.configure(text=self.formula)

if name == ‘main’:

root = Tk()

root["bg"] = "#000"

root.geometry("485x550+200+200")

root.title("Симулятор майнинга")

root.resizable(False, False)

app = Main(root)

app.pack()

root.mainloop()

thx, who can help!

This makes bal a local variable, because it is mutated inside the method.

Make it a variable in the class, like self.formula. Or make it global (not recommended).

Can you show me how to do it? Because I’m completely new.

UnboundLocalError is the error you get inside a function when you try to access a local variable before it has a value. Like this:

def func():
    print(x)  # x has no value
    x = 1

In your logicalc method, you try to use the value of bal before it has a value:

def logicalc(self, operation):
    if operation == "Баланс":
        self.formula = "У вас целых " + str(bal) + " Биткоинов!"
    # more code here
    (bal) = bal - 30

Maybe you need to make bal a global variable?

def logicalc(self, operation):
    global bal
    if operation == "Баланс":
        self.formula = "У вас целых " + str(bal) + " Биткоинов!"
    # more code here
    (bal) = bal - 30
1 Like

Thank you! you really helped me. :slight_smile:

I have struggled with scope as well. I keep this link pinned in case I need a reminder of the way variables are traversed. Python Scope & the LEGB Rule: Resolving Names in Your Code – Real Python

Goodluck!