Skip to content

Commit 697d6aa

Browse files
committed
Update calculator.py
Bugfixes
1 parent 56907f9 commit 697d6aa

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

calculator/api/calculator.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,15 @@ def set_equation(self, first_part: str | float| int, operator: Operator = None,
4545
>>> calc = Calculator()
4646
>>> calc.set_equation(2, Operator.MULTIPLY, 2)
4747
>>> calc.get_equation()
48-
2 x 2
48+
'2 x 2'
4949
"""
5050
self.equation_first = str(first_part)
51-
self.equation_type = operator or ""
5251
self.equation_last = str(second_part)
5352
if operator == None:
53+
self.equation_type = ""
5454
self.current_part = "first"
5555
else:
56+
self.equation_type = operator.value
5657
self.current_part = "last"
5758

5859

@@ -62,7 +63,7 @@ def get_equation(self):
6263
>>> calc = Calculator()
6364
>>> calc.set_equation(2, Operator.MULTIPLY, 2)
6465
>>> calc.get_equation()
65-
2 x 2
66+
'2 x 2'
6667
"""
6768
eq = " ".join([self.equation_first, self.equation_type, self.equation_last])
6869
eq = eq.rstrip()
@@ -76,7 +77,8 @@ def clear_entry(self):
7677
>>> calc.set_equation(2, Operator.MULTIPLY, 2)
7778
>>> calc.clear_entry()
7879
>>> calc.clear_entry()
79-
2
80+
>>> calc.get_equation()
81+
'2'
8082
"""
8183
if self.current_part == "first":
8284
self.equation_first = self.equation_first[:-1]
@@ -100,7 +102,7 @@ def add_digit(self, digit: str | int):
100102
>>> calc.add_digit('2')
101103
>>> calc.add_digit(4)
102104
>>> calc.get_equation()
103-
24
105+
'24'
104106
"""
105107
if self.current_part == "first":
106108
self.equation_first += str(digit)
@@ -115,11 +117,11 @@ def set_operator(self, operator: Operator):
115117
>>> calc.add_digit(2)
116118
>>> calc.set_operator(Operator.ADD)
117119
>>> calc.add_digit(4)
118-
>>> calc.set_operator(Operator.SUB)
120+
>>> calc.set_operator(Operator.SUBTRACT)
119121
>>> calc.get_equation()
120-
2 - 4
122+
'2 - 4'
121123
"""
122-
self.equation_type = operator
124+
self.equation_type = operator.value
123125
self.current_part = "last"
124126

125127

@@ -130,13 +132,13 @@ def flip_sign(self):
130132
>>> calc.add_digit(2)
131133
>>> calc.flip_sign()
132134
>>> calc.get_equation()
133-
-2
135+
'-2'
134136
135137
>>> calc = Calculator()
136138
>>> calc.set_equation(-2)
137139
>>> calc.flip_sign()
138140
>>> calc.get_equation()
139-
2
141+
'2'
140142
"""
141143
if self.current_part == "first":
142144
if self.equation_first.startswith("-"):
@@ -158,7 +160,7 @@ def add_decimal(self):
158160
>>> calc.add_decimal()
159161
>>> calc.add_digit(5)
160162
>>> calc.get_equation()
161-
2.5
163+
'2.5'
162164
"""
163165
if self.current_part == "first":
164166
if not "." in self.equation_first:
@@ -175,17 +177,17 @@ def solve(self):
175177
>>> calc.set_equation(1, Operator.ADD, 1)
176178
>>> calc.solve()
177179
>>> calc.get_equation()
178-
2
180+
'2'
179181
"""
180182
result = float(self.equation_first)
181183

182-
if self.equation_type == Operator.ADD:
184+
if self.equation_type == Operator.ADD.value:
183185
result += float(self.equation_last)
184-
elif self.equation_type == Operator.SUBTRACT:
186+
elif self.equation_type == Operator.SUBTRACT.value:
185187
result -= float(self.equation_last)
186-
elif self.equation_type == Operator.DIVIDE:
188+
elif self.equation_type == Operator.DIVIDE.value:
187189
result /= float(self.equation_last)
188-
elif self.equation_type == Operator.MULTIPLY:
190+
elif self.equation_type == Operator.MULTIPLY.value:
189191
result *= float(self.equation_last)
190192

191193
if result == int(result):

0 commit comments

Comments
 (0)