-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeoquiz.py
154 lines (130 loc) · 7.12 KB
/
geoquiz.py
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import random
import time
from colorama import Fore, Back, Style, init
# Initialize colorama
init(autoreset=True)
# Generate a random authentication code for testing
def generate_auth_code():
return str(random.randint(10000, 99999))
# Store questions
questions = [
{"question": "What is the capital of France?", "options": ["Berlin", "Madrid", "Paris", "Rome"], "answer": "C"},
{"question": "Which country is known as the Land of the Rising Sun?", "options": ["China", "Japan", "South Korea", "Thailand"], "answer": "B"},
{"question": "Which is the largest desert in the world?", "options": ["Sahara", "Arabian", "Gobi", "Antarctic"], "answer": "D"},
{"question": "What is the longest river in the world?", "options": ["Amazon", "Nile", "Yangtze", "Mississippi"], "answer": "B"},
{"question": "Which country has the most number of islands?", "options": ["Finland", "Indonesia", "Sweden", "Greece"], "answer": "A"},
{"question": "Mount Everest is located in which country?", "options": ["Nepal", "China", "India", "Bhutan"], "answer": "A"},
{"question": "What is the smallest country in the world?", "options": ["Monaco", "Vatican City", "San Marino", "Liechtenstein"], "answer": "B"},
{"question": "What is the largest country in the world by area?", "options": ["Canada", "United States", "Russia", "China"], "answer": "C"},
{"question": "Which ocean is the largest?", "options": ["Atlantic", "Indian", "Arctic", "Pacific"], "answer": "D"},
{"question": "Which continent is the Sahara Desert located in?", "options": ["Asia", "Africa", "Australia", "South America"], "answer": "B"},
{"question": "Which river is the longest in Africa?", "options": ["Nile", "Amazon", "Zambezi", "Congo"], "answer": "A"},
{"question": "Which country has the longest coastline?", "options": ["Australia", "Canada", "Russia", "United States"], "answer": "B"},
{"question": "Which mountain range separates Europe and Asia?", "options": ["Himalayas", "Alps", "Ural", "Rockies"], "answer": "C"},
{"question": "Which country has the highest population?", "options": ["India", "United States", "China", "Brazil"], "answer": "C"},
{"question": "What is the longest river in the world?", "options": ["Amazon", "Nile", "Yangtze", "Mississippi"], "answer": "B"},
{"question": "Which country has the most number of islands?", "options": ["Finland", "Indonesia", "Sweden", "Greece"], "answer": "A"},
{"question": "What is the capital of Brazil?", "options": ["Rio de Janeiro", "Sao Paulo", "Brasilia", "Buenos Aires"], "answer": "C"},
{"question": "Which is the largest desert in the world?", "options": ["Sahara", "Arabian", "Gobi", "Antarctic"], "answer": "D"},
{"question": "Which river is the longest in Africa?", "options": ["Nile", "Amazon", "Zambezi", "Congo"], "answer": "A"},
{"question": "Which mountain range separates Europe and Asia?", "options": ["Himalayas", "Alps", "Ural", "Rockies"], "answer": "C"}
]
# Shuffle questions every time quiz starts
random.shuffle(questions)
def display_title():
print(Fore.CYAN + Style.BRIGHT + """
##########################################################
## Welcome to GeoQuiz! ##
## Test your knowledge with geography trivia! ##
##########################################################
""")
def generate_auth_code():
return str(random.randint(10000, 99999))
# Display welcome message
def welcome_message():
print(Fore.YELLOW + """
Welcome to GeoQuiz!
Test your geography knowledge with interesting trivia questions.
Get as many right answers as you can!
""")
# Authentication logic
def authenticate():
auth_code = generate_auth_code()
print(Fore.YELLOW + f"Authentication Code: {auth_code}\n")
while True:
entered_code = input(Fore.YELLOW + "Enter the authentication code to begin the quiz: ")
if entered_code.isdigit() and entered_code == auth_code:
print(Fore.GREEN + "Authentication successful!")
return True
else:
print(Fore.RED + "Invalid authentication code! Please try again.")
def get_valid_input(prompt, valid_inputs):
while True:
user_input = input(prompt).strip().lower()
if user_input in valid_inputs:
return user_input
else:
print(Fore.RED + "Invalid input! Please enter a valid choice.")
def ask_name_and_start():
name = input(Fore.YELLOW + "Please enter your name: ").strip()
start_quiz = get_valid_input(f"Hello {name}, do you want to start the quiz? (y/n): ", ['y', 'n'])
if start_quiz == 'y':
return name
else:
print(Fore.CYAN + "Thank you! Have a great day!")
exit()
def display_score(score):
print(Fore.GREEN + f"Current Score: {score} / 200 points")
def format_time(seconds):
minutes = seconds // 60
seconds = seconds % 60
return f"{minutes} Min {seconds} Sec"
def run_quiz():
score = 0
start_time = time.time()
name = ask_name_and_start()
for i, q in enumerate(questions[:20], 1): # Only 20 questions for the quiz
print(Fore.YELLOW + f"\nQuestion {i}: {q['question']}")
print(Fore.CYAN + f"a) {q['options'][0]}")
print(Fore.CYAN + f"b) {q['options'][1]}")
print(Fore.CYAN + f"c) {q['options'][2]}")
print(Fore.CYAN + f"d) {q['options'][3]}")
print(Fore.YELLOW + f"(Time Allowed: 25 Sec)")
question_start_time = time.time()
answer = get_valid_input(Fore.YELLOW + "Your answer (a/b/c/d): ", ['a', 'b', 'c', 'd'])
time_taken = time.time() - question_start_time
if time_taken > 25:
print(Fore.RED + "Times Up, Moving up to Next Question!")
continue
correct_answer = q['answer']
correct_option = q['options'][ord(correct_answer) - 65] # Convert 'A' to '0', 'B' to '1', etc.
if answer == correct_answer.lower():
score += 10
display_score(score)
print(Fore.GREEN + "Correct! +10 points\n")
else:
display_score(score)
print(Fore.RED + f"Incorrect! The correct answer was {correct_option}.")
time.sleep(2) # Pause for a second before showing the next question
# Calculate total time taken
total_time_taken = int(time.time() - start_time)
final_time = format_time(total_time_taken)
# Final score and time
print(Fore.CYAN + f"\nQuiz Completed! Your final score is: {score}/200 points.")
print(Fore.YELLOW + f"Time Taken: {final_time}")
# Display remarks based on score
if score <= 50:
print(Fore.RED + "Better luck next time. Keep learning!")
elif 51 <= score <= 100:
print(Fore.GREEN + "Good job! You're improving!")
elif 101 <= score <= 150:
print(Fore.GREEN + "Great work! You're quite knowledgeable!")
else:
print(Fore.CYAN + "Excellent! You're a Geography master!")
def main():
display_title()
welcome_message()
if authenticate():
run_quiz()
if __name__ == "__main__":
main()