-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.py
161 lines (122 loc) · 4.58 KB
/
project.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
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
from PyInquirer import prompt
from tabulate import tabulate
from quiz import QuestionDashboard, Results
import json, random
import pandas as pd
import sys
class QuizzardApp:
''' Quizzard Main UI'''
def __init__(self, user_name) -> None:
self.user_name = user_name
self.menu_options = [
{
'type': 'list',
'name': 'choice',
'message': f'Welcome, {self.user_name} to CS50 - QuizzardApp',
'choices': [
'Start Quiz',
'View High Scores',
'Exit'
]
}
]
def run(self):
self.show_menu()
def show_menu(self):
answers = prompt(self.menu_options)
choice = answers['choice']
if choice == 'Start Quiz':
self.start_quiz()
elif choice == 'View High Scores':
self.view_high_scores()
elif choice == 'Exit':
print("Exiting QuizzardApp...")
sys.exit(0)
def start_quiz(self):
print(f"\nGood luck, {self.user_name}!\n")
with open('./data/questions.json') as f:
q1 = json.loads(f.read())
q1 = random.sample(list(q1), 10)
for question in q1:
random.shuffle(question['choices'])
NewQuestionBoard = QuestionDashboard(self.user_name, q1)
user_data = NewQuestionBoard.show_question_and_gen_userdata()
self.result = Results(user_data, [j['answer'] for j in q1])
self.show_post_quiz_results()
def show_post_quiz_results(self):
print("\n")
response = prompt([
{
'type': 'list',
'name': 'choice',
'message': "Do you want to view your results and claim your certificate?",
'choices': ["Yes", "No, Back to Menu"]
}
])
self.result.dump_scores()
if response['choice'] == 'Yes':
# !Important methods to that nees to be executed
self.result.show()
self.result.show_certificate_dialog()
menu_with_no_start = self.menu_options
menu_with_no_start[0]['choices'].pop(0)
print("--------------------------------")
ch = prompt(menu_with_no_start)['choice']
if ch == 'View High Scores':
self.view_high_scores()
elif ch == "Exit":
print("Exiting...")
else:
menu_with_no_start = self.menu_options
menu_with_no_start[0]['choices'].pop(0)
print("\n")
ch = prompt(menu_with_no_start)['choice']
if ch == 'View High Scores':
self.view_high_scores()
elif ch == "Exit":
print("Exiting...")
def view_high_scores(self):
with open('data/scores.json', 'r') as file:
scores = json.load(file)
df = pd.DataFrame(scores)
print("""
__ __ ___ _____ _____ _____ _____ _____ _____
/ | \/___\ ___ / ___>/ \/ _ \/ _ \/ __\/ ___>
| _ || |<___>|___ || |--|| | || _ <| __||___ |
\__|__/\___/ <_____/\_____/\_____/\__|\_/\_____/<_____/
""")
try:
df = df.sort_values(by="score", ascending=False)
df = df.reset_index(drop=True)
df = df.rename_axis('Rank')
df.columns = df.columns.str.title()
df.index = df.index + 1
table = tabulate(df, headers='keys', tablefmt='grid')
print(table)
except:
print("No scores data.")
# Entry point of the program
if __name__ == "__main__":
print("""\n
__/\__
. _ \/\/''//
-( )-/_||_\/
.'. \_()_/
| | . \/
|mrf| . \/
.'. ,\_____'.
_______ __ __ ___ _______ _______ _______ ______ ______
| || | | || | | || || _ || _ | | |
| _ || | | || | |____ ||____ || |_| || | || | _ |
| | | || |_| || | ____| | ____| || || |_||_ | | | |
| |_| || || | | ______|| ______|| || __ || |_| |
| | | || | | |_____ | |_____ | _ || | | || |
|____||_||_______||___| |_______||_______||__| |__||___| |_||______|
\n
----------------------------------------------
A python-based quiz game.
Author: journelcabrillos@gmail.com
----------------------------------------------
\n""")
app = QuizzardApp(input("Type name: "))
app.run()