Python Program to Create a Quiz Game with Questions and Score
for loop to ask each question, get user input with input(), check answers, and keep score.Examples
How to Think About It
input(), compare it to the correct answer, and keep track of how many they get right. At the end, show the total score.Algorithm
Code
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)}.")Dry Run
Let's trace a user answering '4' and 'blue' to the quiz questions.
Initialize questions and score
questions = {'What is 2+2?': '4', 'What color is the sky?': 'blue'}, score = 0
Ask first question
User sees: 'Question: What is 2+2? ' and inputs '4'
Check first answer
'4'.lower() == '4'.lower() is True, print 'Correct!' and score = 1
Ask second question
User sees: 'Question: What color is the sky? ' and inputs 'blue'
Check second answer
'blue'.lower() == 'blue'.lower() is True, print 'Correct!' and score = 2
Show final score
Print 'You scored 2 out of 2.'
| Question | User Answer | Correct Answer | Score After |
|---|---|---|---|
| What is 2+2? | 4 | 4 | 1 |
| What color is the sky? | blue | blue | 2 |
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
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)}.")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)}.")
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.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Dictionary of questions | O(n) | O(n) | Simple quizzes with direct question-answer pairs |
| List of dictionaries | O(n) | O(n) | Quizzes needing extra info per question |
| Function-based | O(n) | O(n) | Cleaner, reusable code for larger projects |
.lower() on user input and answers to make checking answers case-insensitive.