-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplaymaths.py
167 lines (124 loc) · 3.58 KB
/
playmaths.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
162
163
164
165
166
from colorama import init
from mysql import connector as msql
from termcolor import colored
import time
init()
def time_msg(s,clr='cyan'):
for i in s:
print(colored(i,clr),end='',flush=True)
time.sleep(0.01)
return ''
def tabulize(record, clr='light_cyan'): #To tabulize and make data segsy
l=[]
for i in record:
l.append(i[0])
s=len(l)
lines=s*16
print(colored('+'+'-'*lines+'+',clr))
for i in range(s):
print(colored('|'+ str(i).center(15),clr),end='')
print(colored('|',clr))
print(colored('+'+'-'*lines+'+',clr))
for i in range(s):
p=l[i].capitalize()
print(colored('|'+ p.center(15),clr),end='')
print(colored('|',clr))
print(colored('+'+'-'*lines+'+',clr))
def get_quiz(): #To display all the tables in the database and also gets the question to answer
con = msql.connect(
host="localhost",
user="root",
password="surya@2006",
database="maths"
)
cursor=con.cursor()
cursor.execute('SHOW TABLES')
records=cursor.fetchall()
tabulize(records,'magenta')
print()
p=int(input(colored("Enter the quiz_id of the quiz you want to answer: ",'magenta')))
cursor.close()
con.close()
return p
def convert_sql_list(record):
l=[]
for i in record:
l.append(i[0])
return l
def play_quiz():
score=0
con = msql.connect(
host="localhost",
user="root",
password="surya@2006",
database="maths"
)
cursor=con.cursor()
cursor.execute('SHOW TABLES')
record=cursor.fetchall()
l=convert_sql_list(record)
g=get_quiz()
cursor.execute(
'''SELECT Q_ID
FROM {} '''.format(l[g]))
q_id=cursor.fetchall()
q_id=convert_sql_list(q_id)
cursor.execute(
'''SELECT QUESTIONS
FROM {} '''.format(l[g]))
questions=cursor.fetchall()
questions=convert_sql_list(questions)
cursor.execute(
'''SELECT A
FROM {} '''.format(l[g]))
option_A=cursor.fetchall()
option_A=convert_sql_list(option_A)
cursor.execute(
'''SELECT B
FROM {} '''.format(l[g]))
option_B=cursor.fetchall()
option_B=convert_sql_list(option_B)
cursor.execute(
'''SELECT C
FROM {} '''.format(l[g]))
option_C=cursor.fetchall()
option_C=convert_sql_list(option_C)
cursor.execute(
'''SELECT D
FROM {} '''.format(l[g]))
option_D=cursor.fetchall()
option_D=convert_sql_list(option_D)
cursor.execute(
'''SELECT ANSWER
FROM {} '''.format(l[g]))
answer=cursor.fetchall()
answer=convert_sql_list(answer)
cursor.close()
con.close()
# print(q_id)
# print(questions)
# print(option_A)
# print(option_B)
# print(option_C)
# print(option_D)
#print(answer[1])
print()
useless=input(time_msg('Press any key when you are ready: '))
print()
for i in range(len(q_id)):
s1='\033[1m({})'.format(q_id[i]), '\033[1m{}'.format(questions[i])
s2='(A) {}'.format(option_A[i]), ' (B) {}'.format(option_B[i]),' (C) {}'.format(option_C[i]),' (D) {}'.format(option_D[i])
time_msg(s1,'blue')
print()
time_msg(s2,'blue')
ans=input(' ')
if ans == answer[i]:
time_msg("Your answer was right", 'green')
score+=1
else:
time_msg("Your answer was wrong", 'red')
score-=1
print('\n')
print('\n')
time_msg('Your score is: '+str(score), 'green')
return score