Skip to content

Commit 77c9f67

Browse files
Merge pull request steam-bell-92#540 from Tech4Aditya/main
Enhance calculator with history tracking and input updates
2 parents e1ae0ee + 9c9789f commit 77c9f67

1 file changed

Lines changed: 64 additions & 33 deletions

File tree

math/Simple-Calculator/Simple-Calculator.py

Lines changed: 64 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import math
22

3-
from matplotlib.pylab import angle
4-
53
class Calculator:
64
def __init__(self):
7-
pass
5+
self.history = []
86

97
def start_calculation(self):
108
print("--------Menu---------")
@@ -23,11 +21,13 @@ def start_calculation(self):
2321
print('13. Cotangent')
2422
print('14. Secant')
2523
print('15. Cosecant')
26-
print('16. Exit')
24+
print('16. View History')
25+
print('17. Exit')
2726

2827
while True:
2928
try:
30-
user_input = int(input('Enter your choice to calculate(1-16): '))
29+
user_input = int(input('Enter your choice to calculate(1-17): '))
30+
3131
if user_input == 1:
3232
self.addition()
3333
elif user_input == 2:
@@ -59,13 +59,15 @@ def start_calculation(self):
5959
elif user_input == 15:
6060
self.cosecant()
6161
elif user_input == 16:
62+
self.show_history()
63+
elif user_input == 17:
6264
print('Goodbye! Have a good day.')
6365
break
6466
else:
6567
print('Wrong Choice.')
6668

6769
except ValueError:
68-
print('please enter correct option.')
70+
print('Please enter correct option.')
6971
except ZeroDivisionError:
7072
print('Zero cannot be divided by zero.')
7173
except Exception as e:
@@ -76,108 +78,137 @@ def clean(self, x, eps=1e-10):
7678
return 0.0
7779
return x
7880

81+
def add_history(self, operation):
82+
self.history.append(operation)
83+
84+
def show_history(self):
85+
if not self.history:
86+
print("No calculations yet.")
87+
else:
88+
print("\n------ Calculation History ------")
89+
for item in self.history:
90+
print(item)
7991

8092
def addition(self):
81-
a = int(input('enter first value: '))
82-
b = int(input('enter second value: '))
93+
a = float(input('Enter first value: '))
94+
b = float(input('Enter second value: '))
8395
add = a + b
8496
print(f'The Result is: {add}')
85-
97+
self.add_history(f"{a} + {b} = {add}")
98+
8699
def substraction(self):
87-
a = int(input('enter first value: '))
88-
b = int(input('enter second value: '))
100+
a = float(input('Enter first value: '))
101+
b = float(input('Enter second value: '))
89102
minus = a - b
90103
print(f'The Result is: {minus}')
104+
self.add_history(f"{a} - {b} = {minus}")
91105

92-
def division(self):
93-
a = int(input('enter first value: '))
94-
b = int(input('enter second value: '))
95-
div = a / b
96-
print(f'The Result is: {div}')
97-
98106
def multiplication(self):
99-
a = int(input('enter first value: '))
100-
b = int(input('enter second value: '))
107+
a = float(input('Enter first value: '))
108+
b = float(input('Enter second value: '))
101109
multiplication = a * b
102110
print(f'The Result is: {multiplication}')
111+
self.add_history(f"{a} * {b} = {multiplication}")
112+
113+
def division(self):
114+
a = float(input('Enter first value: '))
115+
b = float(input('Enter second value: '))
116+
div = a / b
117+
print(f'The Result is: {div}')
118+
self.add_history(f"{a} / {b} = {div}")
103119

104120
def floor_division(self):
105-
a = int(input('enter first value: '))
106-
b = int(input('enter second value: '))
121+
a = float(input('Enter first value: '))
122+
b = float(input('Enter second value: '))
107123
floor = a // b
108124
print(f'The Result is: {floor}')
125+
self.add_history(f"{a} // {b} = {floor}")
109126

110127
def modulus(self):
111-
a = int(input('enter first value: '))
112-
b = int(input('enter second value: '))
128+
a = float(input('Enter first value: '))
129+
b = float(input('Enter second value: '))
113130
mod = a % b
114131
print(f'The Result is: {mod}')
132+
self.add_history(f"{a} % {b} = {mod}")
115133

116134
def power(self):
117-
a = int(input('enter first value: '))
118-
b = int(input('enter second value: '))
135+
a = float(input('Enter first value: '))
136+
b = float(input('Enter second value: '))
119137
power = a ** b
120138
print(f'The Result is: {power}')
139+
self.add_history(f"{a} ** {b} = {power}")
121140

122141
def factorial(self):
123142
a = int(input("Enter the number: "))
124143
fact = 1
125-
for i in range(1,a+1):
126-
fact = fact * i
144+
for i in range(1, a + 1):
145+
fact *= i
127146
print(f"Factorial is: {fact}")
128-
147+
self.add_history(f"{a}! = {fact}")
148+
129149
def sqrt(self):
130-
num = int(input("Enter the number: "))
150+
num = float(input("Enter the number: "))
131151
sqrt = num ** 0.5
132-
print(f"Square root of {num} : {sqrt}")
152+
print(f"Square root of {num}: {sqrt}")
153+
self.add_history(f"sqrt({num}) = {sqrt}")
133154

134155
def sine(self):
135156
angle = float(input("Enter the angle in degrees: "))
136157
rads = math.radians(angle)
137158
sin_val = self.clean(math.sin(rads))
138159
print(f"Sine of {angle} degrees is: {sin_val}")
160+
self.add_history(f"sin({angle}) = {sin_val}")
139161

140162
def cosine(self):
141163
angle = float(input("Enter the angle in degrees: "))
142164
rads = math.radians(angle)
143165
cos_val = self.clean(math.cos(rads))
144166
print(f"Cosine of {angle} degrees is: {cos_val}")
167+
self.add_history(f"cos({angle}) = {cos_val}")
145168

146169
def tangent(self):
147170
angle = float(input("Enter the angle in degrees: "))
148171
rads = math.radians(angle)
149172
tan_val = self.clean(math.tan(rads))
150173
print(f"Tangent of {angle} degrees is: {tan_val}")
174+
self.add_history(f"tan({angle}) = {tan_val}")
151175

152176
def cotangent(self):
153177
angle = float(input("Enter the angle in degrees: "))
154178
rads = math.radians(angle)
155179
tan_val = self.clean(math.tan(rads))
180+
156181
if tan_val != 0:
157182
cot_val = 1 / tan_val
158183
print(f"Cotangent of {angle} degrees is: {cot_val}")
184+
self.add_history(f"cot({angle}) = {cot_val}")
159185
else:
160-
print(f"Cotangent of {angle} degrees is undefined.")
186+
print("Cotangent is undefined.")
161187

162188
def secant(self):
163189
angle = float(input("Enter the angle in degrees: "))
164190
rads = math.radians(angle)
165191
cos_val = self.clean(math.cos(rads))
192+
166193
if cos_val != 0:
167194
sec_val = 1 / cos_val
168195
print(f"Secant of {angle} degrees is: {sec_val}")
196+
self.add_history(f"sec({angle}) = {sec_val}")
169197
else:
170-
print(f"Secant of {angle} degrees is undefined.")
198+
print("Secant is undefined.")
171199

172200
def cosecant(self):
173201
angle = float(input("Enter the angle in degrees: "))
174202
rads = math.radians(angle)
175203
sin_val = self.clean(math.sin(rads))
204+
176205
if sin_val != 0:
177206
csc_val = 1 / sin_val
178207
print(f"Cosecant of {angle} degrees is: {csc_val}")
208+
self.add_history(f"csc({angle}) = {csc_val}")
179209
else:
180-
print(f"Cosecant of {angle} degrees is undefined.")
210+
print("Cosecant is undefined.")
211+
181212

182213
cal = Calculator()
183214
cal.start_calculation()

0 commit comments

Comments
 (0)