-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz_game.py
More file actions
83 lines (71 loc) · 2.73 KB
/
quiz_game.py
File metadata and controls
83 lines (71 loc) · 2.73 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
import time
print("Welcome to my quiz!")
while True:
playing = input("Do you want to play? ")
if playing.lower() != "yes":
break
print("Okay! Let's play :)")
score = 0
num_questions = 10
timer_duration = 5
for i in range(num_questions):
# ask question
if i == 0:
question = "What year was the Declaration of Independence signed?"
answer = "1776"
elif i == 1:
question = "Who was the first president of the United States?"
answer = "George Washington"
elif i == 2:
question = 'Which state is known as the "Sunshine State"?'
answer = "Florida"
elif i == 3:
question = "What is the tallest mountain in North America?"
answer = "Denali"
elif i == 4:
question = "What is the capital of California?"
answer = "Sacramento"
elif i == 5:
question = 'Which US state is known as the "Lone Star State"?'
answer = "Texas"
elif i == 6:
question = "Who wrote the Star-Spangled Banner?"
answer = "Francis Scott Key"
elif i == 7:
question = 'Which US city is known as the "Windy City"?'
answer = "Chicago"
elif i == 8:
question = "Who invented the telephone?"
answer = "Alexander Graham Bell"
elif i == 9:
question = "What is the name of the river that flows through the Grand Canyon?"
answer = "Colorado River"
print(f"Question {i+1}: {question}")
# start countdown timer
start_time = time.time()
elapsed_time = 0
while elapsed_time < timer_duration:
time_left = timer_duration - elapsed_time
print(f"Time left: {time_left} seconds")
user_answer = input()
elapsed_time = time.time() - start_time
# check if user answer is correct
if user_answer.lower() == answer.lower():
print("Correct!")
score += 1
else:
print("Incorrect.")
percentage_score = (score / num_questions) * 100
if percentage_score < 60:
print(
f"Sorry! You got {score}/{num_questions} correct ({percentage_score:.2f}%). Better luck next time!")
elif percentage_score >= 60 and percentage_score < 80:
print(
f"Congratulations! You got {score}/{num_questions} correct ({percentage_score:.2f}%). Good job!")
else:
print(
f"Excellent work! You got {score}/{num_questions} correct ({percentage_score:.2f}%). Well done!")
play_again = input("Do you want to play again? ")
if play_again.lower() != "yes":
break
print("Thanks for playing!")