1

So i'm writing a program that converts currencies for a school projects. I tried using as many loops and references to make the program as short as possible. But i keep getting error

"UnboundLocalError: local variable 'v3' referenced before assignment".

Here's the code(note: i trimmed it down so it's just the necessary part).

from tkinter import *
root = Tk()
btn1 = StringVar()
btn2 = StringVar()
VALUTE = [ #besedilo v gumbu, oznaka, tečaj
    ("Evro", "evrov", 1),
    ("Dolar", "dolarjev", 1.12005),
    ("Funt", "funtov", 0.739868547),
]
v = v2 = v3 = v4 = 1
def funkcija():
    n = int(entry1.get())
    X = btn1.get()
    Y = btn2.get()
    for text, mark, convert in VALUTE:
        if v3 == X:
            o = convert
            E = mark
        v3 = v3 + 1
        if v4 == Y:
            p = convert
            F = mark
        v4 = v4 + 1
    m = pretvorba(o,p)
    print("%s %s je %s %s." % (n, E, m, F))

def pretvorba(a,b):
    Q = n/a*b
    return Q

for text, mark, convert in VALUTE:
    gumb = Radiobutton(root, text=text, value = v, variable = btn1).grid(row = v+1, column=1, sticky=W)
    v=v+1

for text, mark, convert in VALUTE:
    gumb = Radiobutton(root, text=text, value = v2, variable = btn2).grid(row = v2+6, column=1, sticky=W)
    v2=v2+1

entry1 = Entry(root)
entry1.grid(row=1, column=1, sticky=W)

go = Button(root, text="Izračun", fg="white", bg="black", command=funkcija)
go.grid(row=10, columnspan=3)

root.mainloop()
adricadar
  • 9,971
  • 5
  • 33
  • 46
Zoxx
  • 141
  • 1
  • 1
  • 9
  • 2
    your function `funkcija` create its own `v3` variable and you read it before it is set. You can tell python not to create a local v3 adding `global v3` statement at the beginning of `funkcija` – FabienAndre May 02 '15 at 12:50
  • possible duplicate of [UnboundLocalError in Python](http://stackoverflow.com/questions/9264763/unboundlocalerror-in-python) – FabienAndre May 02 '15 at 12:53

3 Answers3

1

In Python when you assign a value to a variable in a function the compiler assumes that, unless specified, the variable is a local. For example:

x = 42
y = "this is a test"

def foo():
    x = 1     # this is local, unrelated to the external defined one
    print(y)  # this is global

If you want to change a global in a function you need to explicitly inform the compiler of that with global:

def bar():
    global x
    x = 1     # this changes the global variable x
6502
  • 112,025
  • 15
  • 165
  • 265
0

It looks like you call v3 == X before you initialize v3 in the function funkcija(). You'll need to pass v3 to the function funkcija(v3) or define it as a global variable global v3 to access it inside that function

Matthew
  • 672
  • 4
  • 11
  • yes, it would, I wrote it before that bit was editted in, so missed that line. Updated my answer – Matthew May 02 '15 at 13:01
0

Ok so i figured it out. I defined v3 and v4 inside the function def funkcija(). Then I converted X and Y into integers and made n global. It turns out that the if loop wasn't running because X and Y weren't integers.

Here's the fixed code:

v = v2 = 1
def funkcija():
    v3 = v4 = 1
    n = float(entry1.get())
    global n
    X = int(btn1.get())
    Y = int(btn2.get())
    for text, mark, convert in VALUTE:
        if v3 == X:
            o = convert
            E = mark
        v3+=1
        if v4 == Y:
            p = convert
            F = mark
        v4+=1
    m = pretvorba(o,p)
    print("%s %s je %s %s." % (n, E, m, F))

def pretvorba(a,b):
    Q = n/a*b
    return Q
Zoxx
  • 141
  • 1
  • 1
  • 9
  • Hello Zoxx (and welcome on stackoverflow). Glad to see you fixed your issue. This site is more than an internet forum and you might consider to post in the answer part only stuff relevant to the exact problem you raised (your `UnboundLocalError` and local vs global issue). If an answer helped you, you might consider to accept it (green tick sign). Once you will reach 15 points of reputation, you will also be able to upvote (any answer, not only to your question, as well as any question). You could read this [presentation page](http://stackoverflow.com/tour) of StackOverflow. – FabienAndre May 02 '15 at 20:07