-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz.py
More file actions
34 lines (26 loc) · 1.08 KB
/
Copy pathquiz.py
File metadata and controls
34 lines (26 loc) · 1.08 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
import json, random, os
def readJson(path = "questions.json"):
with open(path, encoding='utf-8') as fh:
data = json.load(fh)
return data
def run_quiz(questions):
score = 0
totalQuestions = len(questions)
for question in questions:
randomNum = random.randint(0,len(questions)-1)
print("\n" + questions[randomNum]["question"])
print(len(questions[randomNum]["question"]) * "-")
for key, value in questions[randomNum]["options"].items():
print(f"\n{key}: {value}")
user_answer = input("\nVotre réponse: ").upper()
if user_answer == questions[randomNum]["answer"]:
print("Correct!\n")
score += 1
else:
print("\nIncorrect.\nLa bonne réponse est:", questions[randomNum]["answer"])
questions.pop(randomNum)
input("\nPesez sur ENTER pour la prochaine question")
os.system("cls")
print(f"Votre score est de {score}/{totalQuestions} ({int((score/totalQuestions) * 100)}%).")
input("")
run_quiz(readJson())