-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_checker.py
More file actions
277 lines (236 loc) · 8.73 KB
/
math_checker.py
File metadata and controls
277 lines (236 loc) · 8.73 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#import the os to assist with determining terminal width for the banner, and for the clear screen function used by the actual program
import os
#import time to enable sleep()
import time
#import sys to enable the sys.exit() function
import sys
#define variables for banner text color:
color_red = "\033[1;31;40m \n"
color_green = "\033[1;32;40m \n"
color_white = "\033[1;37;40m \n"
color_cyan = "\033[1;36;40m \n"
color_yellow = "\033[1;33;40m \n"
#create the function for the banner, first argument is the text, second is the color variable from above:
def cent_banner(banner_title, banner_color):
#define variables declaring the ascii art used for the borders:
horz_border = "═"
vert_border = "║"
top_left_corner = "╔"
top_right_corner = "╗"
bot_left_corner = "╚"
bot_right_corner = "╝"
#get the terminal width for the centering function:
size = os.get_terminal_size()
columns = size.columns
title_len = len(banner_title)
#determine if the terminal width is an even or odd number, and if odd, make even:
if (columns % 2) == 0:
columns = columns
else:
columns = columns + 1
#determine if the banner_title is an odd or even number, and if odd, make even:
if (title_len % 2) == 0:
banner_title = banner_title
else:
banner_title = banner_title + " "
#creates box 1/4 the width of the screen:
banner_width = columns // 4
if (banner_width % 2) == 0:
banner_width = banner_width
else:
banner_width = banner_width + 1
#do math to center the banner_title text based on the terminal width:
banner_mid_space = banner_width - title_len
banner_mid_space_half = banner_mid_space // 2
banner_mid_spaces = banner_mid_space_half * " "
#creates the actual box based on the terminal width:
banner_top = top_left_corner + horz_border * banner_width + top_right_corner
banner_mid = vert_border + banner_mid_spaces + banner_title + banner_mid_spaces + vert_border
banner_bot = bot_left_corner + horz_border * banner_width + bot_right_corner
#centers the banner in the terminal window:
cent_banner_top = banner_top.center(columns)
cent_banner_mid = banner_mid.center(columns)
cent_banner_bot = banner_bot.center(columns)
#print the actual banner:
print(banner_color)
print(cent_banner_top)
print(cent_banner_mid)
print(cent_banner_bot)
print(color_white) #resets text color to white so following text won't inherit banner color
#create a function to clear the screen:
def clear_screen():
os.system('clear' if os.name == 'posix' else 'cls')
#define functions for the arithmetic operations:
#addition function
def addition():
while True:
clear_screen()
cent_banner("Addition", color_cyan)
try:
num1 = int(input("Please enter the first number in the problem: "))
num2 = int(input("Please enter the second number in the problem: "))
except ValueError:
print("Invalid input! Please enter an interger.")
time.sleep(3)
continue
result = num1 + num2
try:
answer = int(input(f"What is {num1} plus {num2}? "))
except ValueError:
print("Invalid input! Please enter an interger")
time.sleep(3)
continue
if answer == result:
print("Correct!")
else:
print("I got a different answer, please check your work and try again.")
choice = input("Enter 1 to repeat, 2 to return to the main menu, or 3 to exit...")
if choice == '1':
clear_screen()
continue
elif choice == '2':
clear_screen()
break
elif choice == '3':
clear_screen()
sys.exit()
else:
print("Invalid input, please enter a number between 1-3")
time.sleep(5)
#subtraction function
def subtraction():
while True:
clear_screen()
cent_banner("Subtraction", color_cyan)
try:
num1 = int(input("Please enter the first number in the problem: "))
num2 = int(input("Please enter the second number in the problem: "))
except ValueError:
print("Invalid input! Please enter an interger.")
time.sleep(3)
continue
result = num1 - num2
try:
answer = int(input(f"What is {num1} minus {num2}? "))
except ValueError:
print("Invalid input! Please enter an interger.")
time.sleep(3)
continue
if answer == result:
print("Correct!")
else:
print("I got a different answer, please check your work and try again.")
choice = input("Enter 1 to repeat, 2 to return to the main menu, or 3 to exit...")
if choice == '1':
clear_screen()
continue
elif choice == '2':
clear_screen()
break
elif choice == '3':
clear_screen()
sys.exit()
else:
print("Invalid input, please enter a number between 1-3")
time.sleep(5)
#multiplication function
def multiplication():
while True:
clear_screen()
cent_banner("Multiplication", color_cyan)
try:
num1 = int(input("Please enter the first number in the problem: "))
num2 = int(input("Please enter the second number in the problem: "))
except ValueError:
print("Invalid input! Please enter an interger.")
time.sleep(3)
continue
result = num1 * num2
try:
answer = int(input(f"What is {num1} times {num2}? "))
except ValueError:
print("Invalid input! Please enter an interger.")
time.sleep(3)
continue
if answer == result:
print("Correct!")
else:
print("I got a different answer, please check your work and try again.")
choice = input("Enter 1 to repeat, 2 to return to the main menu, or 3 to exit...")
if choice == '1':
clear_screen()
continue
elif choice == '2':
clear_screen()
break
elif choice == '3':
clear_screen()
sys.exit()
else:
print("Invalid input, please enter a number between 1-3")
time.sleep(5)
#division function
def division():
while True:
clear_screen()
cent_banner("Division", color_cyan)
try:
num1 = int(input("Please enter the first number in the problem: "))
num2 = int(input("Please enter the second number in the problem: "))
except ValueError:
print("Invalid input! Please enter an interger.")
time.sleep(3)
continue
try:
answer = int(input(f"What is {num1} divided by {num2}? "))
answer_remainder = int(input("What remainder did you have? If you didn't have one, enter 0: "))
except ValueError:
print("Invalid input! Please enter an interger.")
time.sleep(3)
continue
result = num1 // num2
remainder_result = num1 % num2
if remainder_result == answer_remainder:
answer = result
else:
answer = "invalid"
if answer == result:
print("Correct!")
else:
print("I got a different answer, please check your work and try again.")
choice = input("Enter 1 to repeat, 2 to return to the main menu, or 3 to exit...")
if choice == '1':
clear_screen()
continue
elif choice == '2':
clear_screen()
break
elif choice == '3':
clear_screen()
sys.exit()
else:
print("Invalid input, please enter a number between 1-3")
time.sleep(5)
#Main menu function
while True:
clear_screen()
cent_banner("Math Checker", color_cyan)
print("1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Exit")
choice = input("Please enter your selection (1-5): ")
if choice == '1':
addition()
elif choice == '2':
subtraction()
elif choice == '3':
multiplication()
elif choice == '4':
division()
elif choice == '5':
sys.exit()
else:
print("Invalid input, please enter a number between 1-5")
time.sleep(5)