Python Mini-Project: Quiz Game

Goal of the Day

Today you combine everything you’ve learned:

By the end, you will have a small game that you can expand.

Step 1: Basic Quiz Structure


questions = {
    "What is the capital of Germany?": "Berlin",
    "2+2 = ?": "4",
    "Is Python a programming language?": "Yes"
}

points = 0

for question in questions:
    answer = input(question + " ")
    if answer == questions[question]:
        points += 1
        print("Correct!")
    else:
        print("Wrong!")

print("You scored", points, "out of", len(questions), "points!")

Explanation:

Step 2: Improve Answers


answer = input(question + " ").lower()

if answer == questions[question].lower():

👉 .lower() converts everything to lowercase → avoids errors with uppercase/lowercase

Step 3: Categories


quiz = {
    "Math": {
        "2+2 = ?": "4",
        "5*3 = ?": "15"
    },
    "General": {
        "Capital of Germany?": "Berlin"
    }
}

category = input("Choose a category: ")
questions = quiz[category]

Step 4: Difficulty Levels


quiz = {
    "easy": {
        "2+2": "4"
    },
    "hard": {
        "12*12": "144"
    }
}

level = input("Choose difficulty (easy/hard): ")
questions = quiz[level]

Step 5: Save Results


file = open("results.txt", "a")
file.write("Points: " + str(points) + "\n")
file.close()

Step 6: Complete Example


import random

quiz = {
    "easy": {
        "2+2": "4",
        "3+5": "8"
    },
    "hard": {
        "12*12": "144",
        "15*3": "45"
    }
}

level = input("Choose difficulty (easy/hard): ")
questions = quiz[level]

points = 0

for question in questions:
    answer = input(question + " ").lower()
    if answer == questions[question].lower():
        points += 1
        print("Correct!")
    else:
        print("Wrong!")

print("You scored", points, "out of", len(questions), "points!")

# Save results
file = open("results.txt", "a")
file.write("Level: " + level + " Points: " + str(points) + "\n")
file.close()

Exercise (Extension)

Example for randomness:


import random
question = random.choice(list(questions.keys()))