-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz_game.py
More file actions
96 lines (82 loc) · 3.03 KB
/
quiz_game.py
File metadata and controls
96 lines (82 loc) · 3.03 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import time
import json
import os
# Define the quiz questions and answers
questions = [
{
"question": "What is the capital of France?",
"options": ["A. Paris", "B. London", "C. Berlin", "D. Madrid"],
"answer": "A"
},
{
"question": "Which planet is known as the Red Planet?",
"options": ["A. Earth", "B. Mars", "C. Jupiter", "D. Saturn"],
"answer": "B"
},
{
"question": "Who wrote 'To Kill a Mockingbird'?",
"options": ["A. Harper Lee", "B. Mark Twain", "C. J.K. Rowling", "D. Ernest Hemingway"],
"answer": "A"
},
{
"question": "What is the largest ocean on Earth?",
"options": ["A. Atlantic", "B. Indian", "C. Arctic", "D. Pacific"],
"answer": "D"
},
{
"question": "Which element has the chemical symbol 'O'?",
"options": ["A. Gold", "B. Oxygen", "C. Hydrogen", "D. Iron"],
"answer": "B"
}
]
high_scores_file = 'high_scores.json'
def load_high_scores():
if os.path.exists(high_scores_file):
with open(high_scores_file, 'r') as file:
return json.load(file)
return []
def save_high_scores(high_scores):
with open(high_scores_file, 'w') as file:
json.dump(high_scores, file)
def display_high_scores():
high_scores = load_high_scores()
if high_scores:
print("\nHigh Scores: ")
for idx, score in enumerate(high_scores):
print(f"{idx+1}. {score['name']} - {score['score']}")
else:
print("\nNo high scores found.")
def save_high_score(name, score):
high_scores = load_high_scores()
high_scores.append({"name": name, "score": score})
high_scores.sort(key=lambda x: x["score"], reverse=True)
save_high_scores(high_scores[:10]) # Only keep the top 10 high scores
def quiz_game():
score = 0
total_questions = len(questions)
time_limit = 10 # seconds
for i, question in enumerate(questions):
print(f"\nQuestion {i+1}/{total_questions}") # Similar like Question 1/10 to the User interface
print(question['question']) # Under question list, iterate over question
for option in question['options']: # creating an instance named option from options under question dict
print(option)
start_time = time.time()
user_answer = input("Enter your answer (A, B, C, D): ").upper()
elapsed_time = time.time() - start_time
if elapsed_time > time_limit:
print("TIme's up!")
continue # to the next question
if user_answer == question['answer']:
print("Correct!")
score += 1
else:
print(f"Wrong! The correct answer is {question['answer']}")
print(f"\nYour final score is: {score}/{total_questions}") # like 7/10
name = input("Enter your name: ")
save_high_score(name, score)
display_high_scores()
if __name__ == "__main__":
print("Welcome to the Quiz Game!")
display_high_scores()
quiz_game()
print("Thank you for playing!")