Handle Wrong Answers
Exit

Handle Wrong Answers

Add an else branch to print feedback when the player's answer is wrong

💻

Writing code and entering commands is only available on desktop. Open this page on a larger screen to complete this chapter.

When the player answers correctly, the quiz prints "Correct!" But when they are wrong, nothing happens — the program ends silently. You need to handle that case.

The else clause

An else clause runs when the if condition is False. Together, if and else cover every possible outcome:

if condition:
    # runs when True
else:
    # runs when False

Exactly one branch runs — never both, never neither.

Applying it to PyQuiz

When is_correct is False, tell the player which option was correct:

if is_correct:
    print("Correct!")
else:
    print(f"Wrong! The correct answer was option {correct}.")

correct holds the number of the right option. For example, if correct is 3, the player should have chosen option 3.

Instructions

Add a wrong-answer message to the existing if statement.

  1. After print("Correct!"), add an else clause that calls print(f"Wrong! The correct answer was option {correct}.") — the else block runs when is_correct is False, so every answer now gets a response: correct answers see "Correct!", wrong answers see the right option number.