我得到"本地变量'回答'在转让前引用"我的代码错误。我该如何解决这个问题?
from Tkinter import *
import Tkinter
def proces():
F=Entry.get(E1)
m=Entry.get(E2)
a=Entry.get(E3)
if F ==0:
answer=float(m)*float(a)
if m ==0:
answer=float(F)/float(a)
if a ==0:
answer=float(F)/float(m)
Entry.insert(E4,0,answer)
print(answer)
#And the rest of the GUI coding goes here.
top = Tkinter.Tk()
L1 = Label(top, text="My calculator",).grid(row=0,column=1)
L2 = Label(top, text="Force",).grid(row=1,column=0)
L3 = Label(top, text="Weight",).grid(row=2,column=0)
L4 = Label(top, text="Accelaration",).grid(row=3,column=0)
L4 = Label(top, text="Solution",).grid(row=4,column=0)
E1 = Entry(top, bd =5)
E1.grid(row=1,column=1)
E2 = Entry(top, bd =5)
E2.grid(row=2,column=1)
E3 = Entry(top, bd =5)
E3.grid(row=3,column=1)
E4 = Entry(top, bd =5)
E4.grid(row=4,column=1)
B=Button(top, text ="Solve",command = proces).grid(row=5,column=1,)
top.mainloop()
答案 0 :(得分:0)
这样给:
answer = "the value which you can give as default"
if F ==0:
answer=float(m)*float(a)
if m ==0:
answer=float(F)/float(a)
if a ==0:
answer=float(F)/float(m)
或
if F ==0:
answer=float(m)*float(a)
elif m ==0:
answer=float(F)/float(a)
elif a ==0:
answer=float(F)/float(m)
else :
answer = "the value which you can give as default"
如果所有条件都不匹配您要分配给answer
的值?您可以在上述答案中提到的那个值
答案 1 :(得分:0)
您正在if语句中为变量'answer'赋值,因此其范围仅限于那些if语句,只需在if语句之前初始化变量答案。
答案 2 :(得分:0)
非常感谢您的快速回复。
我终于解决了它。
我所要做的就是做 if(variable)== 0: 至 如果len(变量)== 0:
我希望将来遇到这个问题的人会觉得这很有帮助。