我有一个项目,目前只会问一个问题,然后休息,给我一条消息:
IndexError: list index out of range
错误在第42行,即:
label.config(text=question[q]).
以下是代码:
from tkinter import *
import tkinter as tk
q = -1
count = 0
correct = 0
incorrect = 0
question=["File compression software is an example of.. software", "Each piece of hardware e.g printer, monitor, keyboard, will need an up to date... installed", "application softwares control the computer, true or false?"]
answer = ["utility", "driver", "false"]
answer_cap = ["Utility","Driver","False"]
root = Tk()
name = tk.Label(root,text = "Computing quiz")
name.pack()
label = tk.Label(root,text = question[0])
label.pack()
entry = tk.Entry(root)
entry.pack()
def out():
global q,correct,incorrect,count
while count < len(question):
ans = entry.get()
if answer[q] == ans or answer_cap[q] == ans :
q+=1
entry.delete(0, END)
correct+=1
print(correct)
label.config(text=question[q])
else:
q+=1
entry.delete(0, END)
incorrect+=1
print(incorrect)
label.config(text=question[q])
entry.delete(0, END)
label.config(text = "Correct: "+str(correct) + " Incorrect: "+str(incorrect))
print(correct)
def stop():
global q,correct,incorrect
q = 0
correct = 0
incorrect = 0
entry.delete(0, END)
label.config(text = question[0])
button = tk.Button(root,text = "Submit",command = out)
button.pack()
button_two = tk.Button(root,text = "Restart",command = stop)
button_two.pack()
答案 0 :(得分:2)
查看您的while
循环。您正在执行某些操作while count < ...
,但循环count
内部未更新,但q
已更新。因为这个q
很快会非常大,所以它会比len(question)
高很多。难怪IndexError
你必须纠正那个循环,因为即使你处理IndexError
,while
循环也会永远运行。
一种解决方法(我不建议这样,我认为你应该纠正整个while
循环)可能是except
IndexError
和break
循环