-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz.py
More file actions
90 lines (77 loc) · 2.96 KB
/
quiz.py
File metadata and controls
90 lines (77 loc) · 2.96 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
import os
def load_questions(filename):
questions = []
try:
with open(filename, 'r') as file:
for line in file:
parts = line.strip().split(',')
if len(parts) == 6:
question = parts[0]
options = parts[1:5]
answer = int(parts[5])
questions.append(
{'question': question, 'options': options, 'answer': answer})
except FileNotFoundError:
print(f"{filename} not found. Please create it with questions.")
return questions
def take_quiz(username, questions):
score = 0
for idx, q in enumerate(questions, 1):
print(f"\nQuestion {idx}: {q['question']}")
for i, option in enumerate(q['options'], 1):
print(f"{i}. {option}")
try:
user_answer = int(input("Enter your answer (1-4): "))
if user_answer == q['answer']:
print("Correct!")
score += 1
else:
print(
f"Incorrect! Correct answer: {q['options'][q['answer']-1]}")
except ValueError:
print("Invalid input. Skipping question.")
total = len(questions)
percentage = (score / total) * 100
print(
f"\nQuiz Completed! {username}, your score is {score}/{total} ({percentage:.2f}%).")
save_score(username, score, total)
def save_score(username, score, total):
with open('scores.txt', 'a') as file:
file.write(f"{username},{score},{total}\n")
def view_previous_scores(username):
try:
with open('scores.txt', 'r') as file:
found = False
for line in file:
name, score, total = line.strip().split(',')
if name == username:
print(f"{name} scored {score}/{total}")
found = True
if not found:
print("No previous records found.")
except FileNotFoundError:
print("No scores recorded yet.")
def main():
print("=== Welcome to Online Quiz System ===")
username = input("Enter your username: ")
while True:
print("\n1. Take Quiz")
print("2. View Previous Scores")
print("3. Exit")
choice = input("Enter your choice: ")
if choice == '1':
# the file questions.txt opens
questions = load_questions('questions.txt')
if questions: # if there are questions in the file then
take_quiz(username, questions) # we will take the quiz
else:
print("No questions available.")
elif choice == '2':
view_previous_scores(username)
elif choice == '3':
print("Thank you for using the Online Quiz System.")
break
else:
print("Invalid choice, try again.")
if __name__ == "__main__":
main()