Python 元组循环(我想)
我有这个测试代码,但只有第一个问题出现。我可以回答第一个问题,分数会相应调整,但下一个问题不会出现。我尝试过研究元组循环,并尝试过使用我的代码,但没有效果。我是一个Python新手,希望能得到一些帮助。我已经花了好几个小时了相信我Python 元组循环(我想),python,Python,我有这个测试代码,但只有第一个问题出现。我可以回答第一个问题,分数会相应调整,但下一个问题不会出现。我尝试过研究元组循环,并尝试过使用我的代码,但没有效果。我是一个Python新手,希望能得到一些帮助。我已经花了好几个小时了相信我 from tkinter import * root = Tk() root.title("End of year exam") score = 0 def submit_button_clicked(): global score global
from tkinter import *
root = Tk()
root.title("End of year exam")
score = 0
def submit_button_clicked():
global score
global questions
score = 0
if answer.get() == questions[0][1]:
score+=1
else:
score = score
scoretxt.config(text = "Your score is: {}".format(str(score)))
questions =[("What does 13 + 5 = ?","18"),("What does 12 + 8 = ?","20"),("What does 19 + 6 = ?","25"),("What does 17 + 15 = ?","32")]
Label(root,text = "Question : ",bg ="light grey").grid(row = 0,column = 0, sticky = W)
question=Label(root,bg = "light green",text= questions[1][0], width = 38)
question.grid(row =1,column=0)
Label(root,text = "Answer: ",bg = "light grey").grid(row = 2, column = 0, sticky = W)
answer = Entry(root,bg ="white",width = 45, justify = CENTER)
answer.grid(row = 3,column=0)
scoretxt = Label(root,text ="Your score is:",bg = "light green", width = 38)
scoretxt.grid(row = 10,column = 0, sticky = W)
### make a submit button
Button(root,text= "Submit",bg = "light grey",command = submit_button_clicked).grid(row = 4,column = 0, sticky = W)
root.mainloop()
这应该行得通。我在函数
submit\u button\u clicked()
中添加了try
语句,因此如果问题是最后一个问题,它仍会更新分数。如果你通读代码,你会看到我改变了什么。如果你不明白,就让我说不。祝你今天愉快
from tkinter import *
root = Tk()
root.title("End of year exam")
score = 0
questions =[("What does 13 + 5 = ?","18"),("What does 12 + 8 = ?","20"),("What does 19 + 6 = ?","25"),("What does 17 + 15 = ?","32")]
current_question = 0
def submit_button_clicked():
global score
global questions
global current_question
if answer.get() == questions[current_question][1]:
score += 1
current_question += 1
try:
question.config(text=questions[current_question][0])
except:
pass
answer.delete(0, 'end')
scoretxt.config(text = "Your score is: {}".format(str(score)))
Label(root,text = "Question : ",bg ="light grey").grid(row = 0,column = 0, sticky = W)
question=Label(root,bg = "light green",text=questions[current_question][0], width = 38)
question.grid(row =1,column=0)
Label(root,text = "Answer: ",bg = "light grey").grid(row = 2, column = 0, sticky = W)
answer = Entry(root,bg ="white",width = 45, justify = CENTER)
answer.grid(row = 3,column=0)
scoretxt = Label(root,text ="Your score is:",bg = "light green", width = 38)
scoretxt.grid(row = 10,column = 0, sticky = W)
### make a submit button
Button(root,text= "Submit",bg = "light grey",command = submit_button_clicked).grid(row = 4,column = 0, sticky = W)
root.mainloop()
“只有第一个问题出现”。。嗯,可能是因为你只做
问题[1][0]
?我知道问题[1][0]会让它显示第一个问题,但我不确定如何修改代码以便它显示下一个问题,比如,当单击提交按钮时。我不知道如何更改Tkinter中的标签文本,但您可以保留一个全局变量,用于您当前所问的问题。当您正确回答问题时,您可以增加该计数器,并重置标签中的文本。非常感谢您的帮助。解释一下您的代码是如何不同的,这将是一个更好的答案。是的,读者必须逐行比较您的代码与原始代码,以查看您更改了什么。就个人而言,我不介意逐行检查代码以找出差异,因为这有助于我的理解,但是,我可以看到一些人不想这样做。。非常感谢大家的帮助。如果这解决了你的问题,如果你能接受我的回答,我将不胜感激。谢谢