-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
156 lines (134 loc) · 4.5 KB
/
calculator.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
import sys
print ("\nBasic Calculator Program : ")
def addition(a,b):
return a+b
def subtraction(a,b):
return a-b
def multiplication(a,b):
return a*b
def division(a,b):
return a/b
def modulus(a,b):
return a%b
control = True
while control:
print("\n1. Addition")
print("2. Subtraction")
print("3. Multiplication")
print("4. Division")
print("5. Modulus")
print("6. Quit the program\n")
choice = int (input("Enter your choice: "))
#Cases
if choice==1:
try:
first_number = input("\nFirst Number : ")
if '.' in first_number:
first_number = float(first_number)
else:
first_number = int(first_number)
except ValueError:
print("Error: Invalid Input")
sys.exit(1)
try:
second_number = input("Second Number : ")
if '.' in second_number:
second_number = float(second_number)
else:
second_number = int (second_number)
except ValueError:
print("Error: Invalid Input")
sys.exit(1)
print(addition(first_number,second_number))
print("\n")
if choice==2:
try:
first_number = input("\nFirst Number : ")
if '.' in first_number:
first_number = float(first_number)
else:
first_number = int(first_number)
except ValueError:
print("Error: Invalid Input")
sys.exit(1)
try:
second_number = input("Second Number : ")
if '.' in second_number:
second_number = float(second_number)
else:
second_number = int (second_number)
except ValueError:
print("Error: Invalid Input")
sys.exit(1)
print(subtraction(first_number,second_number))
print("\n")
if choice==3:
try:
first_number = input("\nFirst Number : ")
if '.' in first_number:
first_number = float(first_number)
else:
first_number = int(first_number)
except ValueError:
print("Error: Invalid Input")
sys.exit(1)
try:
second_number = input("Second Number : ")
if '.' in second_number:
second_number = float(second_number)
else:
second_number = int (second_number)
except ValueError:
print("Error: Invalid Input")
sys.exit(1)
print(multiplication(first_number,second_number))
print("\n")
if choice==4:
try:
first_number = input("\nFirst Number : ")
if '.' in first_number:
first_number = float(first_number)
else:
first_number = int(first_number)
except ValueError:
print("Error: Invalid Input")
sys.exit(1)
try:
second_number = input("Second Number : ")
if '.' in second_number:
second_number = float(second_number)
else:
second_number = int (second_number)
except ValueError:
print("Error: Invalid Input")
sys.exit(1)
try:
print(division(first_number,second_number))
except ZeroDivisionError:
print("Cannot divide by zero")
#No sys.exit(1) for division , let user do other mathematical operations or do division after validating input values
print("\n")
if choice==5:
try:
first_number = input("\nFirst Number : ")
if '.' in first_number:
first_number = float(first_number)
else:
first_number = int(first_number)
except ValueError:
print("Error: Invalid Input")
sys.exit(1)
try:
second_number = input("Second Number : ")
if '.' in second_number:
second_number = float(second_number)
else:
second_number = int (second_number)
except ValueError:
print("Error: Invalid Input")
sys.exit(1)
print(modulus(first_number,second_number))
print("\n")
if choice==6:
control = False
print("Quiting the program\n")