0
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?

abcdefg
  • 1
  • 3
  • 1
    you should put your while loop inside the for loop. Don't you wanna stop when all questions have been answered? – Tranbi Oct 04 '21 at 20:32
  • ``` def run_test(questions): score = 0 for question in questions: answer = input(question.prompt) while answer != 'a' or 'b' or 'c': response = input('You did not enter a,b or c. Please try again') if answer == question.answer: score += 1 print('You got' + str(score) + '/' + str(len(questions)) + 'Correct') ``` – abcdefg Oct 04 '21 at 20:39
  • I have done this but it still does not work. I have adjusted it too be inside the for loop but it still does not work as its meant too. – abcdefg Oct 04 '21 at 20:42
  • Even if i type 'a' 'b' or 'c' it now comes up saying that i did not type it in which is slightly confusing. – abcdefg Oct 04 '21 at 20:45
  • Your current logic tells the user you didn't type a valid answer even when it's just the wrong answer. You need an answer loop with something like `while not answer in ["a","b","c"]:` with the question inside the loop. – RufusVS Oct 04 '21 at 20:58
  • `answer != 'a' or 'b' or 'c'` does not do what you think it does, that is equivalent to `(answer != 'a') or bool('b') or bool('c')` and your typical object is always bool(x)==true unless len(x)==0 or x==None or x defined a `__bool__` special method... what you want here is `answer not in ['a', 'b', 'c']` – Copperfield Oct 04 '21 at 20:58
  • Ok that makes alot more sense. However once im then asked to type in either 'a', 'b' or 'c' again after typing in the wrong letter it says that I have not typed in 'a', 'b' or 'c' even though I have. – abcdefg Oct 04 '21 at 21:02
  • I really appreciate the help – abcdefg Oct 04 '21 at 21:03
  • Here is an example – abcdefg Oct 04 '21 at 21:04
  • What color are apples? (a) Red/Green (b) Purple (c) Orange d You did not enter a,b or c. Please try againa You did not enter a,b or c. Please try again – abcdefg Oct 04 '21 at 21:04
  • Wow I have got it now – abcdefg Oct 04 '21 at 21:08

0 Answers0