-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
64 lines (51 loc) · 1.9 KB
/
main.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
from bs4 import BeautifulSoup
BLUE = "\033[0;34m"
BOLD = "\033[1m"
LIGHT_GREEN = "\033[1;32m"
LIGHT_BLUE = "\033[1;34m"
YELLOW = "\033[33m"
END = "\033[0m"
class QuizQuestion:
def __init__(self, text, choices, right, is_raw):
if is_raw:
self.text = text
self.choices = [item[3:].strip() for item in choices.split('\n') if item[3:].strip()]
self.right = right.replace('The correct answer is:', '').strip()
else:
self.text = text
self.choices = choices
self.right = right
def main():
html_doc = open('01.html').read()
soup = BeautifulSoup(html_doc, 'html.parser')
questions = soup.find_all(class_='qtext')
answers = soup.find_all(class_='answer')
right_answers = soup.find_all(class_='rightanswer')
quiz = []
for i in range(len(questions)):
question = questions[i].get_text()
choices = answers[i].get_text()
right_answer = right_answers[i].get_text()
exists = False
for i in quiz:
if question == i.text:
exists = True
if not exists:
quiz_q = QuizQuestion(question, choices, right_answer, True)
quiz.append(quiz_q)
while len(quiz) > 0:
print(YELLOW + f"{len(quiz)} questions to go, Keep going!" + END)
for question in quiz:
choices = []
for i in range(len(question.choices)):
choices.append(chr(65+i) + " " + question.choices[i])
choices = "\n".join(choices)
print(f'\n{BLUE + BOLD + question.text + END}\n{LIGHT_GREEN + choices}')
user_choice = input(BOLD + LIGHT_BLUE + 'Choose: ' + END)
try:
if question.choices[ord(user_choice[0].upper()) - 65] == question.right:
quiz.remove(question)
except:
pass
if __name__ == "__main__":
main()