11"""
22Class that handles the input and calculations
33"""
4-
5- # Calculator Class
64class Calculator :
75 def __init__ (self , calc_display , clear_button , label_offset ):
86 self ._calc_display = calc_display
97 self ._clear_button = clear_button
108 self ._label_offset = label_offset
11- self ._all_clear ()
9+ self .all_clear ()
1210
13- def _calculate (self , number_one , operator , number_two ):
11+ def calculate (self , number_one , operator , number_two ):
1412 result = eval (number_one + operator + number_two )
1513 if int (result ) == result :
1614 result = int (result )
1715 return str (result )
1816
19- def _all_clear (self ):
17+ def all_clear (self ):
2018 self ._accumulator = "0"
2119 self ._operator = None
2220 self ._equal_pressed = False
23- self ._clear_entry ()
21+ self .clear_entry ()
2422
25- def _clear_entry (self ):
23+ def clear_entry (self ):
2624 self ._operand = None
2725 self ._set_button_ce (False )
2826 self ._set_text ("0" )
@@ -44,9 +42,9 @@ def _get_text(self):
4442 def add_input (self , input ):
4543 try :
4644 if input == "AC" :
47- self ._all_clear ()
45+ self .all_clear ()
4846 elif input == "CE" :
49- self ._clear_entry ()
47+ self .clear_entry ()
5048 elif self ._operator is None and input == "0" :
5149 pass
5250 elif len (input ) == 1 and 48 <= ord (input ) <= 57 :
@@ -66,7 +64,7 @@ def add_input(self, input):
6664 self ._operand = display_text
6765 self ._set_button_ce (True )
6866 self ._equal_pressed = False
69- elif input == "+" or input == "-" or input == "/" or input == "x" :
67+ elif input in ( '+' , '-' , '/' , 'x' ) :
7068 if input == "x" :
7169 input = "*"
7270 if self ._equal_pressed :
@@ -76,7 +74,7 @@ def add_input(self, input):
7674 else :
7775 # Perform current calculation before changing inputs
7876 if self ._operand is not None :
79- self ._accumulator = self ._calculate (self ._accumulator , self ._operator , self ._operand )
77+ self ._accumulator = self .calculate (self ._accumulator , self ._operator , self ._operand )
8078 self ._set_text (self ._accumulator )
8179 self ._operand = None
8280 self ._operator = input
@@ -88,22 +86,16 @@ def add_input(self, input):
8886 self ._set_button_ce (True )
8987 self ._equal_pressed = False
9088 elif input == "+/-" :
91- self ._set_text (self ._calculate (self ._get_text (), "*" , "-1" ))
89+ self ._set_text (self .calculate (self ._get_text (), "*" , "-1" ))
9290 elif input == "%" :
93- self ._set_text (self ._calculate (self ._get_text (), "/" , "100" ))
91+ self ._set_text (self .calculate (self ._get_text (), "/" , "100" ))
9492 elif input == "=" :
9593 if self ._operator is not None :
9694 if self ._operand is None :
9795 self ._operand = self ._get_text ()
98- self ._accumulator = self ._calculate (self ._accumulator , self ._operator , self ._operand )
96+ self ._accumulator = self .calculate (self ._accumulator , self ._operator , self ._operand )
9997 self ._set_text (self ._accumulator )
10098 self ._equal_pressed = True
101- # For Debugging
102- print ("\n ----------------" )
103- print ("Input: {}" .format (input ))
104- print ("Accumulator: {}" .format (self ._accumulator ))
105- print ("Operator: {}" .format (self ._operator ))
106- print ("Operand: {}" .format (self ._operand ))
107- except (ZeroDivisionError , RuntimeError ) as error :
108- self ._all_clear ()
99+ except (ZeroDivisionError , RuntimeError ):
100+ self .all_clear ()
109101 self ._set_text ("Error" )
0 commit comments