Lesson Complete!

Quiz Functions

You finished the third lesson of Upgrade PyQuiz. The quiz now has three clean functions, and the main loop is just four lines.

Here is the complete quiz.py:

QUESTIONS = [
    ("What is the type of the value 3.14?", "int", "str", "float", "bool", 3),
    ("What keyword defines a function in Python?", "for", "def", "if", "class", 2),
    ("What does print() do?", "Stores a value", "Returns a number", "Displays output", "Reads input", 3),
]

def ask_question(text, o1, o2, o3, o4, correct):
    print(text)
    print()
    print(f"  1) {o1}")
    print(f"  2) {o2}")
    print(f"  3) {o3}")
    print(f"  4) {o4}")
    print()
    answer_text = input("Your answer (1-4): ")
    answer = int(answer_text)
    is_correct = answer == correct
    print()
    print(f"You answered: {answer}")
    if is_correct:
        print("Correct!")
    else:
        print(f"Wrong! The correct answer was option {correct}.")
    return is_correct

def show_results(score, total):
    print(f"Score: {score}/{total}")
    if score == total:
        print("Perfect score!")
    elif score >= 2:
        print("Great job!")
    elif score >= 1:
        print("Not bad!")
    else:
        print("Keep practicing!")

def run_quiz(questions=QUESTIONS):
    total = len(questions)
    score = 0
    streak = 0
    print("=== Python Quiz ===")
    print()
    for text, o1, o2, o3, o4, correct in questions:
        result = ask_question(text, o1, o2, o3, o4, correct)
        if result:
            score = score + 1
            streak = streak + 1
            if streak >= 3:
                print(f"  Hot streak! ({streak} in a row)")
        else:
            streak = 0
    show_results(score, total)

while True:
    run_quiz()
    print()
    again = input("Play again? (y/n): ")
    if again != "y":
        break

print("Thanks for playing!")

What you added:

  • ask_question — handles all question logic and returns True or False
  • show_results — takes score and total, prints the score line and grade message
  • run_quiz — wraps one full round with a default parameter for the question list
  • Default parametersquestions=QUESTIONS lets you call run_quiz() with no arguments
  • len() — counts items dynamically so total updates when the question list grows

What's still missing: type abc instead of a number and the quiz crashes with a ValueError. In the next lesson, you will use try/except to catch that error and keep the quiz running.