Variable referenced before assignment - Python

Viewed 6308

I am getting the error...

a = a + b
UnboundLocalError: local variable 'a' referenced before assignment

I don't understand why the error occurs if I have assigned the two variables a and b at the start.

from tkinter import *

a = 10
b = 12
def stopProg(e):
    root.destroy()

def addNumbers(e):
    a = a + b
    label1.configure(text= str(a))

root=Tk()
button1=Button(root,text="Exit")
button1.pack()
button1.bind('<Button-1>',stopProg)
button2=Button(root,text="Add numbers")
button2.pack()
button2.bind('<Button-1>',addNumbers)
label1=Label(root,text="Amount")
label1.pack()

root.mainloop()
3 Answers
Related