-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
565 lines (503 loc) · 13 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
from os import name, system
from time import sleep
from sys import exit
import pathlib
# Get pi
pi = float(pathlib.Path('pi.txt').read_text())
# Get title banner
banner = pathlib.Path('banner.txt').read_text()
# Clear the screen
def clear():
if name == 'nt':
system('cls')
else:
system('clear')
# Check if the number ends in .0
def check_dot_zero(num):
return str(num).endswith('.0')
# Perimeter
def p():
try:
clear()
print('To calculate the perimeter of a square, choose 1')
print('To calculate the perimeter of a rectangle, choose 2')
print('To calculate the perimeter of a triangle, choose 3')
print('To calculate the circumference of a circle, choose 4')
print('\nTo return to main menu, choose 9')
sleep(1.5)
choice = input('Enter your choice: ')
if choice == '1':
p_square()
elif choice == '2':
p_rec()
elif choice == '3':
p_tri()
elif choice == '4':
p_circ()
elif choice == '9':
menu()
else:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return p()
except KeyboardInterrupt:
exit(0)
# Perimeter of a square
def p_square():
try:
clear()
side = float(input('Enter the side length: '))
result = side * 4
if check_dot_zero(result) == True:
print(f'The perimeter of the square is: {int(result)}')
else:
print(f'The perimeter of the square is: {round(result, 3)}')
except ValueError:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return p_square()
except KeyboardInterrupt:
exit(0)
# Perimeter of a rectangle
def p_rec():
try:
clear()
length = float(input('Enter the length: '))
width = float(input('Enter the width: '))
result = 2 * (length + width)
if check_dot_zero(result) == True:
print(f'The perimeter of the rectangle is: {int(result)}')
else:
print(f'The perimeter of the rectangle is: {round(result, 3)}')
except ValueError:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return p_rec()
except KeyboardInterrupt:
exit(0)
# Perimeter of a triangle
def p_tri():
try:
clear()
equal = input('Are all of the sides equal? Enter your choice (y/n): ')
if equal == 'y':
clear()
side_len = float(input('Enter the side length: '))
result = side_len * 3
clear()
if check_dot_zero(result) == True:
print(f'The perimeter of the triangle is: {int(result)}')
else:
print(f'The perimeter of the triangle is: {round(result, 3)}')
if equal == 'n':
clear()
a = float(input('Enter the length of a: '))
b = float(input('Enter the length of b: '))
c = float(input('Enter the length of c: '))
result = a + b + c
clear()
if check_dot_zero(result) == True:
print(f'The perimeter of the triangle is: {int(result)}')
else:
print(f'The perimeter of the triangle is: {round(result, 3)}')
except ValueError:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return p_tri()
except KeyboardInterrupt:
exit(0)
# Circumference of a circle
def p_circ():
try:
clear()
r = float(input('Enter the radius: '))
result = 2 * pi * r
if check_dot_zero(result) == True:
print(f'The circumference of the circle is: {int(result)}')
else:
print(f'The circumference of the circle is: {round(result, 3)}')
except ValueError:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return p_circ()
except KeyboardInterrupt:
exit(0)
# Area
def a():
try:
clear()
print('To calculate the area of a square, choose 1')
print('To calculate the area of a rectangle, choose 2')
print('To calculate the area of a triangle, choose 3')
print('To calculate the area of a parallelogram, choose 4')
print('To calculate the area of a trapezoid, choose 5')
print('To calculate the area of a circle, choose 6')
print('\nTo return to main menu, choose 9')
sleep(1.5)
choice = input('Enter your choice: ')
if choice == '1':
a_square()
elif choice == '2':
a_rec()
elif choice == '3':
a_tri()
elif choice == '4':
a_parallel()
elif choice == '5':
a_trap()
elif choice == '6':
a_circ()
elif choice == '9':
menu()
else:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return a()
except KeyboardInterrupt:
exit(0)
# Area of a square
def a_square():
try:
clear()
side = float(input('Enter the side length: '))
result = side ** 2
if check_dot_zero(result) == True:
print(f'The area of the square is: {int(result)}')
else:
print(f'The area of the square is: {round(result), 3}')
except ValueError:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return a_square()
except KeyboardInterrupt:
exit(0)
# Area of a rectangle
def a_rec():
try:
clear()
length = float(input('Enter the length: '))
width = float(input('Enter the width: '))
result = length * width
if check_dot_zero(result) == True:
print(f'The area of the rectangle is: {int(result)}')
else:
print(f'The area of the rectangle is: {round(result, 3)}')
except ValueError:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return a_rec()
except KeyboardInterrupt:
exit(0)
# Area of a triangle
def a_tri():
try:
clear()
base = float(input('Enter the base: '))
height = float(input('Enter the height: '))
result = (base * height) / 2
if check_dot_zero(result) == True:
print(f'The area of the triangle is: {int(result)}')
else:
print(f'The area of the triangle is: {round(result, 3)}')
except ValueError:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return a_tri()
except KeyboardInterrupt:
exit(0)
# Area of a parallelogram
def a_parallel():
try:
clear()
base = float(input('Enter the base: '))
height = float(input('Enter the height: '))
result = base * height
if check_dot_zero(result) == True:
print(f'The area of the parallelogram is: {int(result)}')
else:
print(f'The area of the parallelogram is: {round(result, 3)}')
except ValueError:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return a_parallel()
except KeyboardInterrupt:
exit(0)
# Area of a trapezoid
def a_trap():
try:
clear()
top = float(input('Enter the length of a: '))
bottom = float(input('Enter the length of b: '))
height = float(input('Enter the height: '))
result = ((top + bottom) / 2) * height
if check_dot_zero(result) == True:
print(f'The area of the trapezoid is: {int(result)}')
else:
print(f'The area of the trapezoid is: {round(result, 3)}')
except ValueError:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return a_trap()
except KeyboardInterrupt:
exit(0)
# Area of a circle
def a_circ():
try:
clear()
r = float(input('Enter the radius: '))
result = pi * r ** 2
if check_dot_zero(result) == True:
print(f'The area of the circle is: {int(result)}')
else:
print(f'The area of the circle is: {round(result, 3)}')
except ValueError:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return a_trap()
except KeyboardInterrupt:
exit(0)
# Volume
def v():
try:
clear()
print('To calculate the volume of a cube, choose 1')
print('To calculate the volume of a prism, choose 2')
print('To calculate the volume of a square pyramid, choose 3')
print('To calculate the volume of a cone, choose 4')
print('To calculate the volume of a cylinder, choose 5')
print('To calculate the volume of a sphere, choose 6')
print('\nTo return to main menu, choose 9')
sleep(1.5)
choice = input('Enter your choice: ')
if choice == '1':
v_cube()
elif choice == '2':
v_prism()
elif choice == '3':
v_pyramid()
elif choice == '4':
v_cone()
elif choice == '5':
v_cylinder()
elif choice == '6':
v_sphere()
elif choice == '9':
menu()
else:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return v()
except KeyboardInterrupt:
exit(0)
# Volume of a cube
def v_cube():
try:
clear()
side = float(input('Enter the length of one of the sides: '))
result = side ** 3
if check_dot_zero(result) == True:
print(f'The volume of the cube is: {int(result)}')
else:
print(f'The volume of the cube is: {round(result, 3)}')
except ValueError:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return v_cube()
except KeyboardInterrupt:
exit(0)
# Volume of a prism
def v_prism():
try:
clear()
print('To calculate the volume of a rectangular prism, choose 1')
print('To calculate the volume of a triangular prism, choose 2')
print('\nTo return to the main menu, choose 9')
sleep(1.5)
choice = input('Enter your choice: ')
if choice == '1':
v_prism_cls().rec()
elif choice == '2':
v_prism_cls().tri()
elif choice == '9':
menu()
except KeyboardInterrupt:
exit(0)
class v_prism_cls:
def __init__(self):
try:
return
except KeyboardInterrupt:
exit(0)
# Volume of a rectangular prism
def rec(self):
try:
clear()
length = float(input('Enter the length: '))
width = float(input('Enter the width: '))
height = float(input('Enter the height: '))
result = length * width * height
if check_dot_zero(result) == True:
print(f'The volume of the rectangular prism is: {int(result)}')
else:
print(f'The volume of the rectangular prism is: {round(result, 3)}')
except ValueError:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return v_prism_cls().rec()
except KeyboardInterrupt:
exit(0)
# Volume of a triangular prism
def tri(self):
try:
clear()
base = float(input('Enter the length of the base: '))
height = float(input('Enter the height: '))
length = float(input('Enter the length: '))
result = (base * height * length) / 2
if check_dot_zero(result) == True:
print(f'The volume of the triangular prism is: {int(result)}')
else:
print(f'The volume of the triangular prism is: {round(result, 3)}')
except ValueError:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return v_prism_cls().tri()
except KeyboardInterrupt:
exit(0)
# Volume of a pyramid
def v_pyramid():
try:
clear()
length = float(input('Enter the length: '))
width = float(input('Enter the width: '))
height = float(input('Enter the height: '))
result = (length * width * height) / 3
if check_dot_zero(result) == True:
print(f'The volume of the pyramid is: {int(result)}')
else:
print(f'The volume of the pyramid is: {round(result, 3)}')
except ValueError:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return v_pyramid()
except KeyboardInterrupt:
exit(0)
# Volume of a cone
def v_cone():
try:
clear()
radius = float(input('Enter the radius: '))
height = float(input('Enter the height: '))
result = (pi * radius ** 2) * (height / 3)
if check_dot_zero(result) == True:
print(f'The volume of the cone is: {int(result)}')
else:
print(f'The volume of the cone is: {round(result, 3)}')
except ValueError:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return v_cone()
except KeyboardInterrupt:
exit(0)
# Volume of a cylinder
def v_cylinder():
try:
clear()
radius = float(input('Enter the radius: '))
height = float(input('Enter the height: '))
result = pi * radius ** 2 * height
if check_dot_zero(result) == True:
print(f'The volume of the cylinder is: {int(result)}')
else:
print(f'The volume of the cylinder is: {round(result, 3)}')
except ValueError:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return v_cylinder()
except KeyboardInterrupt:
exit(0)
# Volume of a sphere
def v_sphere():
try:
clear()
radius = float(input('Enter the radius: '))
result = (4 / 3) * pi * radius ** 3
if check_dot_zero(result) == True:
print(f'The volume of the sphere is: {int(result)}')
else:
print(f'The volume of the sphere is: {round(result, 3)}')
except ValueError:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return v_sphere()
except KeyboardInterrupt:
exit(0)
# Menu
def menu():
try:
clear()
print(banner + '\n')
sleep(1)
print('To calculate the perimeter of something, choose 1')
print('To calculate the area of something, choose 2')
print('To calulate the volume of something, choose 3')
print('\nTo exit, choose 9')
sleep(1.5)
choice = input('Enter your choice and press "Enter": ')
if choice == '1':
p()
elif choice == '2':
a()
elif choice == '3':
v()
elif choice == '9':
clear()
print('Goodbye!')
sleep(2)
exit(0)
else:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return menu()
except KeyboardInterrupt:
exit(0)
# Run again
def run_again():
sleep(1.5)
answer = input('\nWould you like to run the program again?\nEnter your choice (y/n): ').lower()
if answer == 'y':
menu()
elif answer == 'n':
clear()
print('Goodbye!')
sleep(2)
exit(0)
else:
sleep(1)
print('Invalid choice!')
sleep(1.5)
return run_again()
# Run the program
menu()
run_again()