元组循环(我认为)

时间:2016-02-20 20:29:56

标签: python

我有一个测验代码,但只出现第一个问题。我可以回答第一个问题,分数会相应调整,但下一个问题永远不会出现。我已经尝试过研究通过元组循环并且使用我的代码但是无济于事。我是一个Python新手,并希望得到一些帮助。我花了好几个小时相信我。

number

1 个答案:

答案 0 :(得分:0)

这应该有效。我在函数submit_button_clicked()中添加了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}}