如何在测验中添加下一个和上一个 [python]
How to add next and previous in a quiz [python]
我有以下代码可将列表转换为多项选择测验:
def practise_test(test):
score = 0
for question in test:
while True:
print(question[question])
reaction = input (question[options] + '\n')
if reaction == question[answer]:
print ('\nright answered!')
print('The answer is indeed {}. {}\n'.format(question[answer], question[explanation]))
score += 1
break
else:
print ('\nIncorrect answer.')
print ('The correct answer is {}. {}\n'.format(question[answer], question[explanation]))
break
print (score)
但现在我必须向 practise_test
函数添加一些代码,这样当您键入: 'previous' 时,您会转到上一个问题。有人有解决这个问题的想法吗?
您可以将 for 循环替换为 while 循环并更改索引 i,以防用户写入 "previous"。顺便提一句。你正在使用的双循环对我来说似乎是不必要的。
example_test = [
{"question": "1 + 1", "answer": "2", "explanation": "math", "options": "2, 4, 6, 8"},
{"question": "2 + 2", "answer": "4", "explanation": "math", "options": "2, 4, 6, 8"},
{"question": "3 + 3", "answer": "6", "explanation": "math", "options": "2, 4, 6, 8"},
{"question": "4 + 4", "answer": "8", "explanation": "math", "options": "2, 4, 6, 8"},
]
def practise_test(test):
score = 0
i = 0
while i < len(test):
question = test[i]
print(question["question"])
reaction = input(question["options"] + '\n')
if reaction == question["answer"]:
print ('\nright answered!')
print('The answer is indeed {}. {}\n'.format(question["answer"], question["explanation"]))
score += 1
elif reaction.startswith("previous"):
i -= 2
i = max(i, -1)
else:
print ('\nIncorrect answer.')
print ('The correct answer is {}. {}\n'.format(question["answer"], question["explanation"]))
i += 1
print (score)
practise_test(example_test)
似乎还缺少的是将字典键用作字符串,但这只是猜测,因为我不知道其余的实现。
我有以下代码可将列表转换为多项选择测验:
def practise_test(test):
score = 0
for question in test:
while True:
print(question[question])
reaction = input (question[options] + '\n')
if reaction == question[answer]:
print ('\nright answered!')
print('The answer is indeed {}. {}\n'.format(question[answer], question[explanation]))
score += 1
break
else:
print ('\nIncorrect answer.')
print ('The correct answer is {}. {}\n'.format(question[answer], question[explanation]))
break
print (score)
但现在我必须向 practise_test
函数添加一些代码,这样当您键入: 'previous' 时,您会转到上一个问题。有人有解决这个问题的想法吗?
您可以将 for 循环替换为 while 循环并更改索引 i,以防用户写入 "previous"。顺便提一句。你正在使用的双循环对我来说似乎是不必要的。
example_test = [
{"question": "1 + 1", "answer": "2", "explanation": "math", "options": "2, 4, 6, 8"},
{"question": "2 + 2", "answer": "4", "explanation": "math", "options": "2, 4, 6, 8"},
{"question": "3 + 3", "answer": "6", "explanation": "math", "options": "2, 4, 6, 8"},
{"question": "4 + 4", "answer": "8", "explanation": "math", "options": "2, 4, 6, 8"},
]
def practise_test(test):
score = 0
i = 0
while i < len(test):
question = test[i]
print(question["question"])
reaction = input(question["options"] + '\n')
if reaction == question["answer"]:
print ('\nright answered!')
print('The answer is indeed {}. {}\n'.format(question["answer"], question["explanation"]))
score += 1
elif reaction.startswith("previous"):
i -= 2
i = max(i, -1)
else:
print ('\nIncorrect answer.')
print ('The correct answer is {}. {}\n'.format(question["answer"], question["explanation"]))
i += 1
print (score)
practise_test(example_test)
似乎还缺少的是将字典键用作字符串,但这只是猜测,因为我不知道其余的实现。