-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path19_simple_quiz_game.py
More file actions
48 lines (42 loc) · 1.5 KB
/
19_simple_quiz_game.py
File metadata and controls
48 lines (42 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def run_quiz(questions):
"""
Runs a multiple-choice quiz, keeps score, and displays the final result.
"""
# 2. Initialize score
score = 0
# 3. Loop through the questions
for i, q in enumerate(questions):
print(f"\nQuestion {i + 1}: {q['question']}")
for option in q['options']:
print(option)
# 4. Prompt for answer and check if correct
user_answer = input("Your answer (A, B, C, or D): ").upper()
if user_answer == q['answer']:
print("Correct!")
# 5. Update score
score += 1
else:
print(f"Wrong! The correct answer was {q['answer']}.")
# 6. Display final score
print(f"\nQuiz finished! Your final score is {score}/{len(questions)}.")
if __name__ == "__main__":
# 1. Create a list of questions
quiz_questions = [
{
"question": "What is the capital of France?",
"options": ["A. London", "B. Berlin", "C. Paris", "D. Madrid"],
"answer": "C"
},
{
"question": "Which planet is known as the Red Planet?",
"options": ["A. Earth", "B. Mars", "C. Jupiter", "D. Venus"],
"answer": "B"
},
{
"question": "What is the largest mammal in the world?",
"options": ["A. Elephant", "B. Blue Whale", "C. Giraffe", "D. Great White Shark"],
"answer": "B"
}
]
print("--- Simple Quiz Game ---")
run_quiz(quiz_questions)