0
0
PythonProgramBeginner · 2 min read

Python Program to Create a Quiz Game with Questions and Score

You can create a quiz game in Python by defining questions and answers in a dictionary, then using a for loop to ask each question, get user input with input(), check answers, and keep score.
📋

Examples

InputUser answers all questions correctly
OutputQuestion: What is 2+2? 4 Correct! Question: What color is the sky? blue Correct! You scored 2 out of 2.
InputUser answers some questions incorrectly
OutputQuestion: What is 2+2? 3 Wrong! The correct answer is 4. Question: What color is the sky? blue Correct! You scored 1 out of 2.
InputUser inputs empty answer
OutputQuestion: What is 2+2? Wrong! The correct answer is 4. Question: What color is the sky? green Wrong! The correct answer is blue. You scored 0 out of 2.
🧠

How to Think About It

To build a quiz game, first prepare a list or dictionary of questions with their correct answers. Then, ask the user each question one by one using a loop. Get the user's answer with input(), compare it to the correct answer, and keep track of how many they get right. At the end, show the total score.
📐

Algorithm

1
Create a collection of questions and their correct answers
2
Set the score counter to zero
3
For each question in the collection:
4
Ask the user the question and get their answer
5
Compare the user's answer to the correct answer
6
If correct, increase the score by one
7
After all questions, display the user's total score
💻

Code

python
questions = {
    "What is 2+2?": "4",
    "What color is the sky?": "blue"
}

score = 0
for question, answer in questions.items():
    user_answer = input(f"Question: {question} ")
    if user_answer.lower() == answer.lower():
        print("Correct!")
        score += 1
    else:
        print(f"Wrong! The correct answer is {answer}.")

print(f"You scored {score} out of {len(questions)}.")
Output
Question: What is 2+2? 4 Correct! Question: What color is the sky? blue Correct! You scored 2 out of 2.
🔍

Dry Run

Let's trace a user answering '4' and 'blue' to the quiz questions.

1

Initialize questions and score

questions = {'What is 2+2?': '4', 'What color is the sky?': 'blue'}, score = 0

2

Ask first question

User sees: 'Question: What is 2+2? ' and inputs '4'

3

Check first answer

'4'.lower() == '4'.lower() is True, print 'Correct!' and score = 1

4

Ask second question

User sees: 'Question: What color is the sky? ' and inputs 'blue'

5

Check second answer

'blue'.lower() == 'blue'.lower() is True, print 'Correct!' and score = 2

6

Show final score

Print 'You scored 2 out of 2.'

QuestionUser AnswerCorrect AnswerScore After
What is 2+2?441
What color is the sky?blueblue2
💡

Why This Works

Step 1: Store questions and answers

Using a dictionary lets us pair each question with its correct answer for easy lookup.

Step 2: Loop through questions

The for loop asks each question one by one, making the program interactive.

Step 3: Compare answers ignoring case

Using .lower() on both answers makes the comparison case-insensitive, so 'Blue' and 'blue' both count as correct.

Step 4: Keep and show score

We add 1 to score for each correct answer and print the total at the end to show how well the user did.

🔄

Alternative Approaches

Using a list of dictionaries for questions
python
quiz = [
    {"question": "What is 2+2?", "answer": "4"},
    {"question": "What color is the sky?", "answer": "blue"}
]
score = 0
for item in quiz:
    user_answer = input(f"Question: {item['question']} ")
    if user_answer.lower() == item['answer'].lower():
        print("Correct!")
        score += 1
    else:
        print(f"Wrong! The correct answer is {item['answer']}.")
print(f"You scored {score} out of {len(quiz)}.")
This approach is more flexible if you want to add more info per question, like multiple choices.
Using functions to organize code
python
def ask_question(q, a):
    user_answer = input(f"Question: {q} ")
    if user_answer.lower() == a.lower():
        print("Correct!")
        return 1
    else:
        print(f"Wrong! The correct answer is {a}.")
        return 0

questions = {"What is 2+2?": "4", "What color is the sky?": "blue"}
score = 0
for q, a in questions.items():
    score += ask_question(q, a)
print(f"You scored {score} out of {len(questions)}.")
Functions make the code cleaner and easier to expand or reuse.

Complexity: O(n) time, O(n) space

Time Complexity

The program loops through all questions once, so time grows linearly with the number of questions.

Space Complexity

The program stores all questions and answers in memory, so space grows linearly with the number of questions.

Which Approach is Fastest?

All approaches have similar time and space complexity; using functions or different data structures mainly affects code clarity, not speed.

ApproachTimeSpaceBest For
Dictionary of questionsO(n)O(n)Simple quizzes with direct question-answer pairs
List of dictionariesO(n)O(n)Quizzes needing extra info per question
Function-basedO(n)O(n)Cleaner, reusable code for larger projects
💡
Use .lower() on user input and answers to make checking answers case-insensitive.
⚠️
Beginners often forget to convert user input to lowercase, causing correct answers with different cases to be marked wrong.