-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleArithmeticModule.py
More file actions
112 lines (87 loc) · 3.4 KB
/
Copy pathSimpleArithmeticModule.py
File metadata and controls
112 lines (87 loc) · 3.4 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
#Simple Arithmetic Module
def Welcome():
print("""
*************************************************************
* SIMPLE ARITHMETIC CALCULATOR *
*************************************************************
Welcome! Let's perform some arithmetic calculations.
How to Use the Program:
1. Enter a number when prompted.
2. Select the operation you want to perform by entering the number corresponding to your choice.
--------------------------------------------------------------------------------------
| IMPORTANT: The program does NOT follow BODMAS OR PEDMAS. |
| Operations are evaluated from left to right. |
| Example: 2 + 2 * 4 gives 16(incorrect) instead of 10(correct) |
--------------------------------------------------------------------------------------
""")
Arithmetic_Symbols = {1:'+',2:'-',3:'*',4:'/'}
total, total_output = 0, ''
def PreStart():
global total, total_output
while True:
try:
total = float(input('\nEnter the first number: '))
total_output = str(total)
print(f'Your input till now --> {total_output}')
break
except ValueError:
print('Please enter a valid integer/float')
continue
def addition(add):
global total
total += add
def subtraction(sub):
global total
total -= sub
def multiplication(mul):
global total
total *= mul
def division(div):
if div == 0:
print('Division by 0 is not allowed/defined')
return
else:
global total
total /= div
def StartModule():
global total_output
while True:
print('\nFrom the following, input a choice:')
print('1. Addition \t\t\t2. Subtraction '
'\n3. Multiplication \t\t4. Division '
'\n0. Exit')
try:
user_choice = int(input('Enter your choice (1-4 or 0 to exit): '))
except ValueError:
print('Please enter a valid choice!')
continue
if user_choice == 0:
print(f'\nThe total is: {total_output} ==> {total:.10f}...')
print('''
----------------------------------------
| Exiting the Arithmetic module. |
----------------------------------------
''')
break
elif user_choice < 1 or user_choice > 4:
print('\nPlease enter a valid choice!')
else:
try:
user_input = float(input('Enter a number: '))
except ValueError:
print('Please enter an integer or float')
continue
if user_choice == 1:
addition(user_input)
elif user_choice == 2:
subtraction(user_input)
elif user_choice == 3 :
multiplication(user_input)
elif user_choice == 4:
division(user_input)
total_output += ' ' + Arithmetic_Symbols[user_choice] + ' ' + str(user_input)
print(f'Your current total: {total_output} ==> {total:.10f}')
if __name__ == '__main__':
Welcome()
PreStart()
StartModule()