0

I've been playing about with this for a few hours now and have tried a few variations and am still getting nowhere and am not sure what I'm getting wrong. I'm trying to make it so users can only give input values of a, b or c. If anyone could advise that would be greatly appreciated. Thanks.

def run_quiz(questions):
    """
    Function loops through questions and checks answer given
    against correct answer. If correct answer given
    then score increases by 1.
    """
    score = 0
    for question in questions:
        answer = input(question.prompt)
        answer = answer.lower()
        if answer == question.answer:
            score += 1
            while answer not in {'a', 'b', 'c'}:
                return "Invalid answer, try again"
            else:
                return f"You guessed {answer}"

    print(f"{name} got {score} out of {str(len(questions))} questions correct")
AriRix1410
  • 23
  • 3
  • I think you may be missing a level of indentation. – Chris Apr 05 '22 at 20:18
  • @Chris: It's more than that; the `while` loop body is just a `return`, which makes no sense with the given design no matter the indentation. It *is* legal to have an `else` attached to a `while`, but it only makes sense if the `while` might exit through the loop condition and might exit through a `break`. – ShadowRanger Apr 05 '22 at 20:29

1 Answers1

1

There's no need for those return statements.

def run_quiz(questions):
    score = 0
    for question in questions:
        answer = input(question.prompt).lower()
        while answer not in {'a','b','c'}:
            answer = input("Invalid answer, try again").lower()
        if answer == question.answer:
            score += 1
            print("Correct")
        else:
            print("Incorrect")
    print(f"{name} got {score} out of {len(questions)} questions correct")
001
  • 13,291
  • 5
  • 35
  • 66