【发布时间】:2015-12-09 00:34:51
【问题描述】:
我试图将输入窗口中的值用作数值,但它不允许我根据程序(即使我只是输入数字)将“字符串”转换为浮点值。
简短示例:
输入来自:
etiquette5=Label(eqGroup,text="Insert value for c")
etiquette5.pack(padx=10,pady=5,expand=True,fill=BOTH)
input3=Entry(eqGroup,width=10)
input3.pack()
inputList[0]=input1.get() #To be able to move the inputs from this method I put them into a global list
输入用于:
def equationSolver(self): #For doing the math shenanigans
a=float(inputList[0])#Error here. It is unable to convert the strings that it takes in via the input to float values
b=float(inputList[1])#Error here. It is unable to convert the strings that it takes in via the input to float values
c=float(inputList[2])#Error here. It is unable to convert the strings that it takes in via the input to float values
X1=(-b/2)+(sqrt(pow(b/2,2)-c))
X2=(-b/2)-(sqrt(pow(b/2,2)-c))
globalList[0]=X1 #Once again, putting them into a global list for movement purposes
globalList[1]=X2
你可以在这里找到完整的代码:http://pastebin.com/1Umug0ms
关于如何做到这一点的任何想法?
提前致谢,斯塔根
【问题讨论】:
-
您是否进行了任何调试以验证
inputList[0]是您认为的那样? -
嗯,不。我不知道如何验证(我们从未在课堂上运行过调试会话)。但起初该值以 0 开头(您可以在完整脚本的顶部看到它)。所以它是一个整数,但后来它从输入变成了一个字符串(看起来)。至少发生了什么?
-
您可以在
equationSolver中临时添加一条打印语句来打印出inputList中的内容。我没有看到条目被初始化为零的任何地方。 -
第 8 行和第 9 行。 globalList=[0,1,2,3] #全局列表,以便我可以在方法之间移动内容 inputList=[0,1,2,3,4,5,6 ]
标签: python-2.7 tkinter