我设置了一个从文件打印并设置变量的功能,以便我可以从文件中选择哪一行。我设置q1 = 0,做一个打印行[q1]的函数。然后我改变q1的值并再次执行该功能以打印行[q1],但这次它是一个不同的行。但我得到" IndexError:列表索引超出范围"。它没有超出范围,因为如果我打印行[5]它可以工作,但如果我设置q1 = 5并打印行[q1]它不会? 该功能第一次运行但不是第二次运行。 这是课程,所以我求助于
def qs(): # ask questions
global q1
global a1
global a2
global a3
global a4
line = questions.readlines()
if difficulty == "easy":
print(line[q1])
easyanswers = line[a1], line[a2]
print(easyanswers)
elif difficulty == "medium":
print(line[q1])
mediumanswers = line[a1], line[a2], line[a3]
print(mediumanswers)
elif difficulty == "hard":
print(line[q1])
hardanswers = line[a1], line[a2], line[a3], line[a4]
print(hardanswers)
else:
print("Invalid difficulty selection")
q()
answer = input("Answer: ")
def qnumber(): # does next question
global question
global q1
global a1
global a2
global a3
global a4
if question == 1:
question = question + 1
qs()
qnumber()
elif 1 < question < 6:
question = question + 1
q1 = q1 + 5
question = 1 # track of question number
q1 = 0 # question line number
a1 = 1 # answer 1 line number
a2 = 2 # answer 2 line number
a3 = 3 # answer 3 line number
a4 = 4 # answer 4 line number
qnumber()