import sys
class Question:
def __init__(self, prompt, answer):
self.prompt = prompt
self.answer = answer
question_prompts = [
'What color are apples?\n(a) Red/Green\n(b) Purple\n(c) Orange\n\n',
'What color are bananas?\n(a) Teal\n(b) Magneta\n(c) Yellow\n\n',
'What color are strawberries?\n(a) Yellow\n(b) Red\n(c) Blue\n\n',
]
questions = [
Question(question_prompts[0], 'a'),
Question(question_prompts[1], 'c'),
Question(question_prompts[2], 'b'),
]
def run_test(questions):
score = 0
while True:
for question in questions:
answer = input(question.prompt)
if answer == question.answer:
score += 1
else:
print('You did not enter a,b or c. Please try again')
print('You got' + str(score) + '/' + str(len(questions)) + 'Correct')
def replay():
choice = input("Play again? Enter Yes or No")
if choice == 'Yes':
run_test(questions)
else:
sys.exit(0)
run_test(questions)
replay()
Im trying to repeat the question_prompt if the user does not type 'a', 'b' or 'c' however im struggling to get it to work.
I know I need to use a while loop but not sure where to use it. I know where its currently being used is wrong. Any help would be much appreciated.
How would I also keep on asking the user to play again as at the moment it only works once?