I am very new to programming so possibly its just basic syntax problem i am having or may be unfamiliarity with functions which is limiting my ability but here is the questions. I found following program in a youtube tutorial in which tutor created a multiple choice questions test with 3 possible answers using class method. here is the original code:
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) Magenta\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
for question in questions:
answer = input(question.prompt)
if answer == question.answer:
score +=1
print('You got ' + str(score) + '/' + str(len(questions)) + ' correct')
run_test(questions)
Original code allows the user to attempt each question only once and scores out of a total of 3 in the end.
The Problem:
I want to add a conditional statement/loop (before score counting if statement) to check if the user has entered correct choice of either 'a' or 'b' or 'c'. If the user enters anything other than a or b or c then it must stay in the loop infinitely and ask the user to enter correct choice of either a or b or c.
i tried while loop but it doesn't break, even if user enter correct choice of either a or b or c. Please help