如何在测验[python]中添加下一个和上一个
我有以下代码将列表转换为多项选择题测验:如何在测验[python]中添加下一个和上一个,python,testing,choice,Python,Testing,Choice,我有以下代码将列表转换为多项选择题测验: def practise_test(test): score = 0 for question in test: while True: print(question[question]) reaction = input (question[options] + '\n') if reaction == question[answer]:
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)
但是现在我必须在
practice\u test
函数中添加一些代码,这样当您键入:“previous”时,您就可以转到上一个问题。有人有办法解决这个问题吗?您可以将for循环与while循环交换,并在用户写入“previous”时更改索引i。顺便说一句,你使用的双环对我来说似乎没有必要
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)
示例_测试=[
{“问题”:“1+1”,“答案”:“2”,“解释”:“数学”,“选项”:“2,4,6,8”},
{“问题”:“2+2”,“答案”:“4”,“解释”:“数学”,“选项”:“2,4,6,8”},
{“问题”:“3+3”,“答案”:“6”,“解释”:“数学”,“选项”:“2,4,6,8”},
{“问题”:“4+4”,“回答”:“8”,“解释”:“数学”,“选项”:“2,4,6,8”},
]
def试验(试验):
分数=0
i=0
而i
似乎还缺少的是将字典键用作字符串,但这只是猜测,因为我不知道剩下的实现。到目前为止您尝试了什么?@ktb对于“下一个问题”部分,我删除了while True语句,并添加了if语句:if reaction==“next”:continue。但我不认为这是最好的方法,我也不知道“前一个问题”的方法。当我尝试这个方法时,它会在第一个问题之后停止。你知道如何解决这个问题吗?我添加了一个我认为测试结构的示例,并在字典键中添加了引号。如果现在不起作用,请举例说明测试的外观。