This repository has been archived by the owner on May 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
math_game.py
150 lines (136 loc) · 4.14 KB
/
math_game.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
import math
from random import randint, choice
import time
import sys
print('------------------------')
print('Math Practice!')
print('For Annie from Uncle Gaelen')
print('-----------------------')
def init():
game = input('What game do you want to play? (Type number and press enter to select)\n 1) Multiplication\n 2) Addition\n 3) Subtraction\n 4) Division\n 5) Random!')
try:
if int(game) > 5:
print('Please enter a number between 1 and 5!')
print(game)
init()
except:
print('Please enter a number between 1 and 5!')
print(game)
init()
numq = input('How many questions do you want to do? (Type in how many and press enter)')
try:
int(numq)
except:
print('Enter a number please!')
init()
print('ok, ' +numq+ ' questions')
execute_game(int(game),int(numq))
def execute_game(g,nq):
N1 = [randint(1,10) for n in range(0,nq)]
N2 = [randint(0,10) for n in range(0,nq)]
start = input('Ok! Press Enter to start!')
stime = time.time()
corr_count = 0
dg = False
for i in range(0,len(N1)):
corr = False
if g == 5:
dg = True
g = choice([1,2,3,4])
if g == 1:
corr = mult_game(N1[i],N2[i])
elif g == 2:
corr = add_game(N1[i],N2[i])
elif g == 3:
corr = sub_game(N1[i],N2[i])
elif g == 4:
corr = div_game(N1[i],N2[i])
if dg == True:
g = 5
if corr == True:
corr_count = corr_count + 1
etime = time.time()
end_game(corr_count,nq,stime,etime)
def mult_game(n1,n2):
corr = False
a = input(str(n1) +' X ' + str(n2)+ ' = ')
try:
if int(a) == n1*n2:
print('Correct!')
corr = True
else:
print('Incorrect! The correct answer is '+ str(n1*n2))
except:
print('Incorrect! The correct answer is '+ str(n1*n2))
return corr
def add_game(n1,n2):
corr = False
a = input(str(n1) +' + ' + str(n2)+ ' = ')
try:
if int(a) == n1+n2:
print('Correct!')
corr = True
else:
print('Incorrect! The correct answer is '+ str(n1+n2))
except:
print('Incorrect! The correct answer is '+ str(n1+n2))
return corr
def sub_game(n1,n2):
corr = False
nf = max(n1,n2)
nl = min(n1,n2)
a = input(str(nf) +' - ' + str(nl)+ ' = ')
try:
if int(a) == nf-nl:
print('Correct!')
corr = True
else:
print('Incorrect! The correct answer is '+ str(nf-nl))
except:
print('Incorrect! The correct answer is '+ str(nf-nl))
return corr
def div_game(n1,n2):
if n1 == 0:
n1 = randint(1,10)
corr = False
a = input(str(n1*n2) +' / ' + str(n1)+ ' = ')
try:
if int(a) == n2:
print('Correct!')
corr = True
else:
print('Incorrect! The correct answer is '+ str(n2))
except:
print('Incorrect! The correct answer is '+ str(n2))
return corr
def end_game(corr_count,numq,stime,etime):
print('Game Complete!')
print('You got ' + str(corr_count)+ ' correct out of '+str(numq)+'!')
grade = float(corr_count)/float(numq)
if grade > 0.9:
print('Your grade: A+! :-D')
elif grade >0.8:
print('Your grade: A! :-D')
elif grade >0.7:
print('Your grade: B! :-)')
elif grade >0.6:
print('Your grade: C! :-/')
elif grade >0.5:
print('Your grade: D! :-(')
else:
print('Your grade: F! :-(')
print('Your time was ' + str(round(etime-stime,2))+ ' seconds!')
rate = (etime-stime)/float(numq)
if rate > 6:
print('You could be faster!')
if rate < 3:
print('You are sooooo fast!')
else:
print('You are pretty fast!')
rep = input('Do you want to play again? Type y or n and press enter')
if rep == 'y':
init()
else:
print('Closing!')
sys.exit()
init()