forked from badguyland/MicroQuizMaster
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathquiz.py
More file actions
executable file
·53 lines (45 loc) · 1.74 KB
/
quiz.py
File metadata and controls
executable file
·53 lines (45 loc) · 1.74 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
#!/usr/bin/env python3
import random
import json
import os
from glob import glob
from persistence import *
def main():
print("\nWelcome to MicroQuizMaster")
while True:
print("\n1. Play a Quiz")
print("2. Make a Quiz")
print("3. Quit")
choice = input("Enter choice number: ")
if choice == "3":
break
elif choice == "2":
os.system("python3 quizcreator.py")
elif choice == "1":
filename = input("Enter quiz filepath: ")
try:
questions, title = load_quiz(filename)
except:
print("Invalid filepath")
continue
question_index = 0
score = 0
while question_index < len(questions):
current_question = questions[question_index]
correct_answer = current_question.correctAnswer
wrong_answers = current_question.wrongAnswers
answers = random.sample([correct_answer] + wrong_answers, len(wrong_answers) + 1)
print(f"\nQuestion: {current_question.question}")
for i, answer in enumerate(answers):
print(f"{i+1}. {answer}")
user_answer = input("Select the correct answer: ")
if user_answer.isdigit() and 1 <= int(user_answer) <= len(answers):
if answers[int(user_answer) - 1] == correct_answer:
score += 1
question_index += 1
else:
print("Invalid choice.")
if question_index >= len(questions):
print(f"Quiz completed! Your score: {score}/{len(questions)}")
if __name__ == "__main__":
main()