找到你要的答案

Q:Local variable referenced before assignment but I already assigned it a value

Q:赋值前引用的局部变量,但我已经赋值给它值

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()

所以我写了一个程序,转换货币为学校项目。我尝试使用尽可能多的循环和引用,使程序尽可能短。但我不断得到错误

“unboundlocalerror:局部变量“V3”引用赋值前”。

这里的代码(注:我修剪下来,所以它只是必要的部分)。

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()
answer1: 回答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

在Python,当你给一个变量赋值函数中的编译器假定,除非指定的变量是一个局部。例如:

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

如果要在函数中更改全局,则需要显式地将编译器与全局:

def bar():
    global x
    x = 1     # this changes the global variable x
answer2: 回答2:

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

它看起来像你叫V3 = = x在你初始化函数中的funkcija() V3。你需要通过V3的功能funkcija(V3)或把它定义为一个全局变量全局V3访问它里面的功能

answer3: 回答3:

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

好吧,我想通了。我定义的V3和V4里面的功能,funkcija()。然后我将x和y转换成整数,使n。事实证明,if循环没有运行,因为x和y不是整数。

这里是固定码:

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
python  python-3.x  tkinter