How to add next and previous in a quiz [python]
Question:
I’ve got the following code that converts a list into a multiple choice quiz:
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)
But now I have to add some code to the practise_test
function so that when you type: ‘previous’ you go to the previous question. Anyone have an idea to fix this?
Answers:
You can exchange the for loop with a while loop and change the index i in case the user writes “previous”. Btw. the double loop you are using seems unnecessary to me.
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)
What also seems to be missing is using the dictionary keys as string, but that is just as guess as I do not know the remaining implementation.
I’ve got the following code that converts a list into a multiple choice quiz:
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)
But now I have to add some code to the practise_test
function so that when you type: ‘previous’ you go to the previous question. Anyone have an idea to fix this?
You can exchange the for loop with a while loop and change the index i in case the user writes “previous”. Btw. the double loop you are using seems unnecessary to me.
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)
What also seems to be missing is using the dictionary keys as string, but that is just as guess as I do not know the remaining implementation.