Python 3.x 在python 3中访问if语句时遇到问题
我在写一个数学游戏的测试程序。我正在努力显示用户是否正确回答了一个显示为“正确”的问题(现在在shell中),但即使所有10个问题都正确回答了,它也不会正确显示。我尝试了多种方法,例如来回更改变量,但没有取得任何效果,并且添加了许多print语句以查看if语句是否被访问,这只显示了一个未被访问的内容Python 3.x 在python 3中访问if语句时遇到问题,python-3.x,if-statement,tkinter,tk,Python 3.x,If Statement,Tkinter,Tk,我在写一个数学游戏的测试程序。我正在努力显示用户是否正确回答了一个显示为“正确”的问题(现在在shell中),但即使所有10个问题都正确回答了,它也不会正确显示。我尝试了多种方法,例如来回更改变量,但没有取得任何效果,并且添加了许多print语句以查看if语句是否被访问,这只显示了一个未被访问的内容 def addition_frame_fun (self): #Creates the frame where students will be answering addition qu
def addition_frame_fun (self): #Creates the frame where students will be answering addition questions with the labels and entry box.
self.add_ans_entry=Entry (self.add_frame)
self.add_ans_entry.grid (row=3,column=5,padx=5)
next_btn=Button (self.add_frame,text="Next Question",command=self.generate_add_question)
next_btn.grid (row=12,column=5,sticky=SE)
def generate_add_question (self):
self.count+=1
if self.count <11:
self.num1=random.randint (1,10)
self.num2=random.randint (1,10)
self.num1_str =str (self.num1)
self.num2_str=str (self.num2)
self.total= str(self.num1+self.num2)
def check_ans (self):
if self.count > 0:
self.user_add_ans = str (self.add_ans_entry.get () )
if self.user_add_ans == self.total:
print (self.total)
print (self.user_add_ans)
print("correct")
self.score+=1
def addition_frame_fun(自我):#创建一个框架,学生将在其中使用标签和输入框回答添加问题。
self.add\u ans\u entry=条目(self.add\u框架)
self.add_ans_entry.grid(行=3,列=5,padx=5)
下一个按钮(self.add\u frame,text=“next Question”,command=self.generate\u add\u Question)
下一个网格(行=12,列=5,粘性=SE)
def生成\添加\问题(自我):
self.count+=1
如果self.count为0:
self.user\u add\u ans=str(self.add\u ans\u entry.get())
如果self.user\u add\u ans==self.total:
打印(总数)
打印(自用户添加)
打印(“正确”)
自我评分+=1
与其将self.total
转换为字符串,不如将self.user\u add\u ans
转换为float
然后进行比较。请尝试将此代码缩减为一个字符串。似乎有很多代码不是说明问题所必需的。@bryan完成了工作,谢谢你告诉我。我尝试过这样做,但现在出现了一个值错误。@VikashPatel在赋值之前,条目是否包含文本或其他内容,因为如果你试图将文本转换为浮点(例如float),则会发生此错误('cat')
然后python将引发错误)您可以检查以下链接:[