-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
58 lines (43 loc) · 1.29 KB
/
Copy pathmain.py
File metadata and controls
58 lines (43 loc) · 1.29 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
from requests import get
from html import unescape
from gc import collect
from rich import print
from rich.prompt import Prompt
input = Prompt.ask
from rich.traceback import install
install()
from random import shuffle
def get_questions():
url = f"https://opentdb.com/api.php?amount=10&category=18&difficulty=easy&type=multiple"
res = get(url)
data = res.json()
for questions in data.get("results"):
yield questions
del res, data, url
collect()
score = 0
for question in get_questions():
# print(question)
print(f"[bold white on red]{unescape(question.get("question"))}[/]")
options = [opt for opt in question.get("incorrect_answers")]
correctAns = question.get("correct_answer")
options.append(correctAns)
shuffle(options)
for index, option in enumerate(options):
print(f"[yellow] {index+1}. [/][blue] {unescape(option)} [/]")
choise = input(
"[violet]Choose Correct Option[/]", choices=["1", "2", "3", "4", "Q"]
)
if choise.isnumeric():
choise = int(choise)
if options[choise - 1] == correctAns:
print("ok")
score += 1
else:
print("no")
print(correctAns)
else:
if choise == "Q":
print(score)
exit()
print(score)