From bfdbbbb7d42d54dc7bff2a95c939a7b77d72e749 Mon Sep 17 00:00:00 2001 From: Balla Keerthi Date: Sun, 31 Oct 2021 16:19:11 -0400 Subject: [PATCH 1/7] Added Scientific features and Trig features; Trig units mode, memory and bonus features need to be added --- calctests.py | 52 +++++++++---- calculator.py | 92 ++++++++++++++++++++++- main-app.py | 201 +++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 317 insertions(+), 28 deletions(-) diff --git a/calctests.py b/calctests.py index 1964570..56f8e97 100644 --- a/calctests.py +++ b/calctests.py @@ -1,25 +1,49 @@ +#From Efe + import unittest -from calculator import Calculator +from Calculator import Calculator -class TestStringMethods(unittest.TestCase): +class TestCalculator(unittest.TestCase): + """ When the test case is executed, + setUp() method gets executed first.""" - def test_add(self): - c = Calculator() - self.assertEqual(c.add(3, 3), 6) + def setUp(self): + self.calculator = Calculator() - def test_add2(self): - c = Calculator() - self.assertEqual(c.add(12, -10), 2) + """ test each method in the Calculator class. + assertEqual checks if the Calculator methods returns + the expected value.""" - def test_add3(self): - c = Calculator() - self.assertEqual(c.add(5, 8), 13) + def test_add(self): + self.assertEqual(self.calculator.add(4, 7), 11) def test_sub(self): - c = Calculator() - self.assertEqual(c.sub(9, 3), 6) + self.assertEqual(self.calculator.sub(10, 5), 5) + + def test_mult(self): + self.assertEqual(self.calculator.mult(3, 7), 21) + + def test_div(self): + self.assertEqual(self.calculator.div(10, 2), 5) + + def test_sq(self): + self.assertEqual(self.calculator.sq(2), 4) + + def test_sqrt(self): + self.assertEqual(self.calculator.sqrt(4), 2) + + def test_varexp(self): + self.assertEqual(self.calculator.varexp(10, 2), 100) + + def test_inverse(self): + self.assertEqual(self.calculator.inverse(10), 0.1) + + def test_invert_sign(self): + self.assertEqual(self.calculator.invert_sign(10), -10) -if __name__ == '__main__': +""" runs the test case TestCalculator. executes each test method + defined in the class and gives us the result.""" +if __name__ == "__main__": unittest.main() diff --git a/calculator.py b/calculator.py index 3c85ead..1a4559f 100644 --- a/calculator.py +++ b/calculator.py @@ -1,3 +1,5 @@ +import math + class Calculator: def __init__(self): @@ -7,6 +9,92 @@ def add(self, x, y): return x + y def sub(self, x, y): - return 0 + return x - y + + def mult(self, x, y): + return x * y + + def div(self, x, y): + if y == 0: + print(Err) + return x / y + + def sq(self, x): + return x**2 + + def sqrt(self, x): + return x**(1/2) + + def varexp(self, x, y): + return x**y + + def inverse(self, x): + return 1 / x + + def invert_sign(self, x): + return x * -1 + + + # added by KB + + def switchDisplayMode(self,string_mode,x): + """ + Switch display mode (binary, octal, decimal, hexadecimal) + switchDisplayMode(String mode) should set the display to the mode given + :param string_mode: + :return: + """ + #binary=0 + #counter=0 + #temp=x + if (string_mode == 'bin'): + # while (temp > 0): + # base = 2 + # binary = ((temp % base)) + # print(binary) + return str(bin(int(x)).replace("0b", "")) # STATE TO BE MODIFIED AS NEEDED + elif (string_mode == 'dec'): + return str(round(float(x), 2)) + elif (string_mode == 'oct'): + return str(oct(int(x)).replace("0o", "")) + elif (string_mode == 'hex'): + return str(hex(int(x)).replace("0x", "")) + else: + return str('invalid selection') + + def sin_deg(self, x): + x=math.radians(x) + return math.sin(x) + + def sin_rad(self, x): + return math.sin(x) + + def cos_deg(self, x): + return math.cos(math.radians(x)) + + def cos_rad(self, x): + return math.cos(x) + + def tan_deg(self, x): + return math.tan(math.radians(x)) + + def tan_rad(self, x): + return math.tan(x) + + def inv_sin_deg(self, x): + return math.sin(math.radians(x)) + + def inv_sin_rad(self, x): + return math.sin(x) + + def inv_cos_deg(self, x): + return math.cos(math.radians(x)) + + def inv_cos_rad(self, x): + return math.cos(x) + + def inv_tan_deg(self, x): + return math.tan(math.radians(x)) -# add lots more methods to this calculator class. + def inv_tan_rad(self, x): + return math.tan(x) \ No newline at end of file diff --git a/main-app.py b/main-app.py index a7cc4e2..10fc37b 100644 --- a/main-app.py +++ b/main-app.py @@ -1,5 +1,5 @@ from calculator import Calculator - +import math def getTwoNumbers(): a = float(input("first number? ")) @@ -7,28 +7,205 @@ def getTwoNumbers(): return a, b -def displayResult(x: float): - print(x, "\n") +def getOneNumber(): # get one number instead + a = float(input("first number? ")) + return a + +def switchDisplayModeInput(): #added by KB + """ + Switch display mode (binary, octal, decimal, hexadecimal) + switchDisplayMode() should rotate through the options + :return: + """ + return str(input('Select a display mode - bin, oct, dec, hex: ')) + +def trig_units_mode_input(): #added by KB + """ + Switch trig units mode (Degrees, Radians) + switchUnitsMode() should rotate through the options + :return: + """ + return input('Select deg or rad: ') + +def displayResult(temp_display: float): + print("\nDISPLAY:\n") + print(temp_display) + + +def performCalcLoop(calc, temp_display): # KB - removed none assignment to temp_display + print('\nBOO! Happy Halloween ;) \n\n "Welcome to your Scientific Calculator."') + print("\nHere's a list of choices:") + print('~' * 70) + print("1 : Addition \t\t 12 : MC") + print("2 : Subtraction \t 13 : MRC") + print("3 : Multiplication\t 14 : Sine ") + print("4 : Division \t\t 15 : Cosine ") + print("5 : Square \t 16 : Tangent ") + print("6 : Square Root\t 17 : Inverse Sine") + print("7 : Variable Exponentiation \t 18 : Inverse Cosine") + print("8 : Inverse of Display \t 19 : Inverse Tangent") + print("9 : Invert Sign (+/-)\t 20 : Quit") + print("10 : Switch Display\t 21 : ") + print("11 : M+ \t\t 22 : ") + print('~' * 70) -def performCalcLoop(calc): while True: - choice = input("Operation? ") - if choice == 'q': + try: + choice = input("Enter number to choose your operation:\n") + except: + print("Please enter a valid number.") + + if choice == '20': + print("Thanks for stopping by, have a great day.") break # user types q to quit calulator. - elif choice == 'add': + + elif choice == '1': + a, b = getTwoNumbers() + temp_display = calc.add(a, b) + displayResult(temp_display) + + elif choice == '2': + a, b = getTwoNumbers() + temp_display = calc.sub(a, b) + displayResult(temp_display) + + elif choice == '3': + a, b = getTwoNumbers() + temp_display = calc.mult(a, b) + displayResult(temp_display) + + elif choice == '4': + a, b = getTwoNumbers() + if b == 0: + print("\nDISPLAY:\nErr") + else: + temp_display = calc.div(a, b) + displayResult(temp_display) + + elif choice == '5': + a = getOneNumber() + temp_display = calc.sq(a) + displayResult(temp_display) + + elif choice == '6': + a = getOneNumber() + temp_display = calc.sqrt(a) + displayResult(temp_display) + + elif choice == '7': a, b = getTwoNumbers() - displayResult(calc.add(a, b)) - else: - print("That is not a valid input.") + temp_display = calc.varexp(a, b) + displayResult(temp_display) + + elif choice == '8': + a = getOneNumber() + temp_display = calc.inverse(a) + displayResult(temp_display) + + elif choice == '9': + a = getOneNumber() + temp_display = calc.invert_sign(a) + displayResult(temp_display) + + + + elif choice == '10': #added by KB + # switch display + a = switchDisplayModeInput() + displayResult(calc.switchDisplayMode(a,temp_display)) + + #elif choice == 'M+': + #elif choice == '11': + + + #elif choice == '12''MC': + + #elif choice == '13''MRC': + + elif choice == '14': #added by KB + a = trig_units_mode_input() + x=getOneNumber() + if a == 'deg': + temp_display = calc.sin_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.sin_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + + elif choice == '15': # added by KB + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.cos_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.cos_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + + elif choice == '16': # added by KB + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.tan_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.tan_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + elif choice == '17': #added by KB + a = trig_units_mode_input() + x=getOneNumber() + if a == 'deg': + temp_display = calc.inv_sin_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.inv_sin_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + + elif choice == '18': # added by KB + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.inv_cos_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.inv_cos_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + + elif choice == '19': # added by KB + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.inv_tan_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.inv_tan_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") # main start def main(): calc = Calculator() - performCalcLoop(calc) + performCalcLoop(calc, 0) #KB - passed second input parameter 0 print("Done Calculating.") if __name__ == '__main__': - main() + main() \ No newline at end of file From dc14592ac706c0f29cba0a0f2f517d25b487d737 Mon Sep 17 00:00:00 2001 From: Balla Keerthi Date: Sun, 31 Oct 2021 17:16:16 -0400 Subject: [PATCH 2/7] Already added Scientific features and Trig features; Trig units mode features are added now; memory and bonus features need to be added --- calculator.py | 8 +++++++- main-app.py | 36 +++++++++++++++++++++++++----------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/calculator.py b/calculator.py index 1a4559f..dc7e3ec 100644 --- a/calculator.py +++ b/calculator.py @@ -97,4 +97,10 @@ def inv_tan_deg(self, x): return math.tan(math.radians(x)) def inv_tan_rad(self, x): - return math.tan(x) \ No newline at end of file + return math.tan(x) + + def trig_units_mode_deg_to_rad(self, x): + return math.radians(x) + + def trig_units_mode_rad_to_deg(self, x): + return math.degrees(x) \ No newline at end of file diff --git a/main-app.py b/main-app.py index 10fc37b..6280c8a 100644 --- a/main-app.py +++ b/main-app.py @@ -36,17 +36,22 @@ def performCalcLoop(calc, temp_display): # KB - removed none assignment to temp_ print('\nBOO! Happy Halloween ;) \n\n "Welcome to your Scientific Calculator."') print("\nHere's a list of choices:") print('~' * 70) - print("1 : Addition \t\t 12 : MC") - print("2 : Subtraction \t 13 : MRC") - print("3 : Multiplication\t 14 : Sine ") - print("4 : Division \t\t 15 : Cosine ") - print("5 : Square \t 16 : Tangent ") - print("6 : Square Root\t 17 : Inverse Sine") - print("7 : Variable Exponentiation \t 18 : Inverse Cosine") - print("8 : Inverse of Display \t 19 : Inverse Tangent") - print("9 : Invert Sign (+/-)\t 20 : Quit") - print("10 : Switch Display\t 21 : ") - print("11 : M+ \t\t 22 : ") + print("1 : Addition \t\t 17 : Inverse sine") + print("2 : Subtraction \t 18 : Inverse cosine") + print("3 : Multiplication\t 19 : Inverse tangent") + print("4 : Division \t\t 20 : Trig units mode - Convert Radians to Degrees") + print("5 : Square \t 21 : Trig units mode - Convert Degrees to Radians") + print("6 : Square Root\t 22 : Factorial") + print("7 : Variable Exponentiation \t 23 : Log x") + print("8 : Inverse of Display \t 24 : 10 power x - Inverse nog") + print("9 : Invert Sign (+/-)\t 25 : Ln x - Natural log") + print("10 : Switch Display\t 26 : e power x - Inverse natural log") + print("11 : M+ \t\t 27 : Pi") + print("12 : MC \t\t 28 : Exponentiation constant") + print("13 : MRC \t\t 29: Quit") + print("14 : Sine") + print("15 : Cosine") + print("16 : Tangent") print('~' * 70) @@ -199,6 +204,15 @@ def performCalcLoop(calc, temp_display): # KB - removed none assignment to temp_ else: print("That is not a valid input.") + elif choice == '20': # added by KB + x = getOneNumber() + temp_display = calc.trig_units_mode_deg_to_rad(x) + print(displayResult(temp_display)," radians" + + elif choice == '21': # added by KB + x = getOneNumber() + temp_display = calc.trig_units_mode_rad_to_deg(x) + print(displayResult(temp_display), " degrees" # main start def main(): From 956ebc420be782b82ef2efd5fe9e5a51afd79109 Mon Sep 17 00:00:00 2001 From: Balla Keerthi Date: Sun, 31 Oct 2021 19:47:29 -0400 Subject: [PATCH 3/7] Already added Scientific features, Trig features and Trig units mode features; bonus features are added now; memory features need to be added and some corrections in switch display mode need to be done --- calculator.py | 26 +++++++++++++++++++++++++- main-app.py | 38 ++++++++++++++++++++++++++++++++++++-- 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/calculator.py b/calculator.py index dc7e3ec..99e2842 100644 --- a/calculator.py +++ b/calculator.py @@ -103,4 +103,28 @@ def trig_units_mode_deg_to_rad(self, x): return math.radians(x) def trig_units_mode_rad_to_deg(self, x): - return math.degrees(x) \ No newline at end of file + return math.degrees(x) + + def factorial(self,x ): + return math.factorial(self.state) + + def log(self, x, base): + return math.log(x,base) + + def inverse_log(self,x): + return 10**x + + def ln(self, x): + return math.ln(x) + + def inv_ln(self,x): + return math.exp(x) + + #def logTen(self, self.state): + #return math.log10(self.state) + + def pi(self): + return math.pi + + def e(self): + return math.e \ No newline at end of file diff --git a/main-app.py b/main-app.py index 6280c8a..96eb484 100644 --- a/main-app.py +++ b/main-app.py @@ -207,12 +207,46 @@ def performCalcLoop(calc, temp_display): # KB - removed none assignment to temp_ elif choice == '20': # added by KB x = getOneNumber() temp_display = calc.trig_units_mode_deg_to_rad(x) - print(displayResult(temp_display)," radians" + print(displayResult(temp_display)," radians") elif choice == '21': # added by KB x = getOneNumber() temp_display = calc.trig_units_mode_rad_to_deg(x) - print(displayResult(temp_display), " degrees" + print(displayResult(temp_display), " degrees") + + elif choice == '22':# added by KB + x = getOneNumber() + temp_display = calc.factorial(x) + print(displayResult(temp_display)) + + elif choice =='23':# added by KB + print("Enter your number and base as prompted below\n") + x,b = getTwoNumbers() + temp_display = calc.log(x,b) + print(displayResult(temp_display)) + + elif choice == '24':# added by KB + x = getOneNumber() + temp_display = calc.inverse_log(x) + print(displayResult(temp_display)) + + elif choice == '25':# added by KB + x = getOneNumber() + temp_display = calc.ln(x) + print(displayResult(temp_display)) + + elif choice == '26':# added by KB + x = getOneNumber() + temp_display = calc.inv_ln(x) + print(displayResult(temp_display)) + + elif choice == '27':# added by KB + temp_display = calc.pi() + print(displayResult(temp_display)) + + elif choice == '28':# added by KB + temp_display = calc.e() + print(displayResult(temp_display)) # main start def main(): From 9f5f1cf307c4041eda2ca00b390786c06c0b0635 Mon Sep 17 00:00:00 2001 From: Balla Keerthi Date: Sun, 31 Oct 2021 21:01:59 -0400 Subject: [PATCH 4/7] Already added Scientific features, Trig features and Trig units mode features; bonus features are added now; memory features need to be added; some corrections in switch display mode are done --- calculator.py | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/calculator.py b/calculator.py index 99e2842..bb79525 100644 --- a/calculator.py +++ b/calculator.py @@ -44,21 +44,18 @@ def switchDisplayMode(self,string_mode,x): :param string_mode: :return: """ - #binary=0 - #counter=0 - #temp=x if (string_mode == 'bin'): - # while (temp > 0): - # base = 2 - # binary = ((temp % base)) - # print(binary) - return str(bin(int(x)).replace("0b", "")) # STATE TO BE MODIFIED AS NEEDED + return str(bin(int(x)).replace("0b", "")) + elif (string_mode == 'dec'): return str(round(float(x), 2)) + elif (string_mode == 'oct'): return str(oct(int(x)).replace("0o", "")) + elif (string_mode == 'hex'): return str(hex(int(x)).replace("0x", "")) + else: return str('invalid selection') From a8416dcc01838f8d19643e0161cec905a2387136 Mon Sep 17 00:00:00 2001 From: Balla Keerthi Date: Sun, 31 Oct 2021 21:43:56 -0400 Subject: [PATCH 5/7] Already added Scientific features, Trig features and Trig units mode features; bonus features are added now; some corrections in switch display mode are done; memory features are added but code needs to be revised for errors --- calculator.py | 18 +++++++++++++++++- main-app.py | 18 +++++++++++++----- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/calculator.py b/calculator.py index bb79525..29a9d46 100644 --- a/calculator.py +++ b/calculator.py @@ -1,8 +1,12 @@ import math + class Calculator: + + def __init__(self): + memory = 0.0 pass def add(self, x, y): @@ -124,4 +128,16 @@ def pi(self): return math.pi def e(self): - return math.e \ No newline at end of file + return math.e + + def madd(self, x, memory): + memory = x + memory + return memory + + def mclear(self): + memory=0 + return memory + + def mrecall(self): + return memory + diff --git a/main-app.py b/main-app.py index 96eb484..bcbf398 100644 --- a/main-app.py +++ b/main-app.py @@ -54,6 +54,7 @@ def performCalcLoop(calc, temp_display): # KB - removed none assignment to temp_ print("16 : Tangent") print('~' * 70) + memory = 0.0 while True: try: @@ -120,13 +121,20 @@ def performCalcLoop(calc, temp_display): # KB - removed none assignment to temp_ a = switchDisplayModeInput() displayResult(calc.switchDisplayMode(a,temp_display)) - #elif choice == 'M+': - #elif choice == '11': + elif choice == '11': #added by KB + displayResult(calc.madd(temp_display)) + memory = calc.madd(temp_display) + return memory + elif choice == '12''MC': #added by KB + displayResult(calc.mclear()) + memory = calc.mclear() + return memory - #elif choice == '12''MC': - - #elif choice == '13''MRC': + elif choice == '13''MRC': #added by KB + displayResult(calc.mrecall()) + memory = calc.mrecall() + return memory elif choice == '14': #added by KB a = trig_units_mode_input() From c363ebe36acd9e66ee47f925b1ea5660b9928a47 Mon Sep 17 00:00:00 2001 From: Balla Keerthi Date: Sun, 31 Oct 2021 22:54:06 -0400 Subject: [PATCH 6/7] Already added Scientific features, Trig features, Trig units mode features, bonus features and memory features; resolved errors. Discussing with team members to align code --- calctests.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++- calculator.py | 12 +++------- main-app.py | 56 ++++++++++++++++++++++++-------------------- 3 files changed, 98 insertions(+), 35 deletions(-) diff --git a/calctests.py b/calctests.py index 56f8e97..19affac 100644 --- a/calctests.py +++ b/calctests.py @@ -1,7 +1,7 @@ #From Efe import unittest -from Calculator import Calculator +from calculator import Calculator class TestCalculator(unittest.TestCase): @@ -42,8 +42,71 @@ def test_inverse(self): def test_invert_sign(self): self.assertEqual(self.calculator.invert_sign(10), -10) + def test_sin_deg(self): + self.assertEqual(self.calculator.sin_deg(45), 0.7071067811865475) + + def test_sin_rad(self): + self.assertEqual(self.calculator.sin_rad(45), 0.8509035245341184) + + def test_cos_deg(self): + self.assertEqual(self.calculator.cos_deg(45), 0.7071067811865476) + + def test_cos_rad(self): + self.assertEqual(self.calculator.cos_rad(45), 0.5253219888177297) + + def test_tan_deg(self): + self.assertEqual(self.calculator.tan_deg(45), 0.9999999999999999) + + def test_tan_rad(self): + self.assertEqual(self.calculator.tan_rad(45), 1.6197751905438615) + + def test_inv_sin_deg(self): + self.assertEqual(self.calculator.inv_sin_deg(45), 0.7071067811865475) + + def test_inv_sin_rad(self): + self.assertEqual(self.calculator.inv_sin_rad(45), 0.8509035245341184) + + def test_inv_cos_deg(self): + self.assertEqual(self.calculator.inv_cos_deg(45), 0.7071067811865476) + + def test_inv_cos_rad(self): + self.assertEqual(self.calculator.inv_cos_rad(45), 0.5253219888177297) + + def test_inv_tan_deg(self): + self.assertEqual(self.calculator.inv_tan_deg(45), 0.9999999999999999) + + def test_inv_tan_rad(self): + self.assertEqual(self.calculator.inv_tan_rad(45), 1.6197751905438615) + + def test_trig_units_mode_deg_to_rad(self): + self.assertEqual(self.calculator.trig_units_mode_deg_to_rad(45), 0.7853981633974483) + + def test_trig_units_mode_rad_to_deg(self): + self.assertEqual(self.calculator.trig_units_mode_rad_to_deg(45), 2578.3100780887044) + + def test_factorial(self): + self.assertEqual(self.calculator.factorial(5), 120) + + def test_log(self): + self.assertEqual(self.calculator.log(45, 10), 1.6532125137753435) + + def test_inverse_log(self): + self.assertEqual(self.calculator.inverse_log(3), 1000) + + def test_inv_ln(self): + self.assertEqual(self.calculator.inv_ln(3), 20.085536923187668) + + def test_pi(self): + self.assertEqual(self.calculator.pi(), 3.141592653589793) + + def test_e(self): + self.assertEqual(self.calculator.e(), 2.718281828459045) + + """ runs the test case TestCalculator. executes each test method defined in the class and gives us the result.""" if __name__ == "__main__": unittest.main() +TestCalculator.py +4 KB \ No newline at end of file diff --git a/calculator.py b/calculator.py index 29a9d46..bffd8a7 100644 --- a/calculator.py +++ b/calculator.py @@ -1,10 +1,6 @@ import math - - class Calculator: - - def __init__(self): memory = 0.0 pass @@ -106,8 +102,8 @@ def trig_units_mode_deg_to_rad(self, x): def trig_units_mode_rad_to_deg(self, x): return math.degrees(x) - def factorial(self,x ): - return math.factorial(self.state) + def factorial(self,x): + return math.factorial(x) def log(self, x, base): return math.log(x,base) @@ -116,7 +112,7 @@ def inverse_log(self,x): return 10**x def ln(self, x): - return math.ln(x) + return math.log(x) def inv_ln(self,x): return math.exp(x) @@ -138,6 +134,4 @@ def mclear(self): memory=0 return memory - def mrecall(self): - return memory diff --git a/main-app.py b/main-app.py index bcbf398..788e50a 100644 --- a/main-app.py +++ b/main-app.py @@ -1,14 +1,12 @@ from calculator import Calculator -import math def getTwoNumbers(): a = float(input("first number? ")) b = float(input("second number? ")) return a, b - def getOneNumber(): # get one number instead - a = float(input("first number? ")) + a = float(input("Number? ")) return a def switchDisplayModeInput(): #added by KB @@ -45,26 +43,31 @@ def performCalcLoop(calc, temp_display): # KB - removed none assignment to temp_ print("7 : Variable Exponentiation \t 23 : Log x") print("8 : Inverse of Display \t 24 : 10 power x - Inverse nog") print("9 : Invert Sign (+/-)\t 25 : Ln x - Natural log") - print("10 : Switch Display\t 26 : e power x - Inverse natural log") - print("11 : M+ \t\t 27 : Pi") - print("12 : MC \t\t 28 : Exponentiation constant") - print("13 : MRC \t\t 29: Quit") - print("14 : Sine") + print("10 : Switch Display\t 26 : e power x - Inverse natural log") + print("11 : M+ \t 27 : Pi constant") + print("12 : MC \t 28 : e constant") + print("13 : MRC \t 29: Clear") + print("14 : Sine\t 30: Quit") print("15 : Cosine") print("16 : Tangent") print('~' * 70) + temp_display = 0 + print("DISPLAY:") + print(temp_display) + memory = 0.0 while True: + try: choice = input("Enter number to choose your operation:\n") + except: print("Please enter a valid number.") - if choice == '20': - print("Thanks for stopping by, have a great day.") - break # user types q to quit calulator. + if choice == '30': + break elif choice == '1': a, b = getTwoNumbers() @@ -84,10 +87,11 @@ def performCalcLoop(calc, temp_display): # KB - removed none assignment to temp_ elif choice == '4': a, b = getTwoNumbers() if b == 0: - print("\nDISPLAY:\nErr") + temp_display = "Err" + displayResult(temp_display) else: temp_display = calc.div(a, b) - displayResult(temp_display) + displayResult(temp_display) elif choice == '5': a = getOneNumber() @@ -122,19 +126,20 @@ def performCalcLoop(calc, temp_display): # KB - removed none assignment to temp_ displayResult(calc.switchDisplayMode(a,temp_display)) elif choice == '11': #added by KB - displayResult(calc.madd(temp_display)) - memory = calc.madd(temp_display) - return memory + displayResult(calc.madd(temp_display,memory)) + memory = calc.madd(temp_display, memory) + #return memory - elif choice == '12''MC': #added by KB + elif choice == '12': #added by KB displayResult(calc.mclear()) memory = calc.mclear() - return memory + #return memory - elif choice == '13''MRC': #added by KB - displayResult(calc.mrecall()) - memory = calc.mrecall() - return memory + elif choice == '13': #added by KB + displayResult(memory) + #displayResult(calc.mrecall(memory)) + #memory = calc.mrecall(memory) + #return memory elif choice == '14': #added by KB a = trig_units_mode_input() @@ -225,7 +230,7 @@ def performCalcLoop(calc, temp_display): # KB - removed none assignment to temp_ elif choice == '22':# added by KB x = getOneNumber() temp_display = calc.factorial(x) - print(displayResult(temp_display)) + displayResult(temp_display) elif choice =='23':# added by KB print("Enter your number and base as prompted below\n") @@ -250,11 +255,12 @@ def performCalcLoop(calc, temp_display): # KB - removed none assignment to temp_ elif choice == '27':# added by KB temp_display = calc.pi() - print(displayResult(temp_display)) + displayResult(temp_display) elif choice == '28':# added by KB temp_display = calc.e() - print(displayResult(temp_display)) + displayResult(temp_display) + # main start def main(): From 29b8b63bd9f1a813f099ad5a3ef75cea41b9713e Mon Sep 17 00:00:00 2001 From: Balla Keerthi Date: Mon, 1 Nov 2021 00:03:24 -0400 Subject: [PATCH 7/7] Already added Scientific features, Trig features, Trig units mode features, bonus features and memory features; resolved all errors and aligned code --- main-app.py | 622 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 435 insertions(+), 187 deletions(-) diff --git a/main-app.py b/main-app.py index 788e50a..61b8457 100644 --- a/main-app.py +++ b/main-app.py @@ -1,15 +1,18 @@ from calculator import Calculator + def getTwoNumbers(): - a = float(input("first number? ")) - b = float(input("second number? ")) + a = float(input("First Number? ")) + b = float(input("Second Number? ")) return a, b -def getOneNumber(): # get one number instead + +def getOneNumber(): # get one number instead a = float(input("Number? ")) return a -def switchDisplayModeInput(): #added by KB + +def switchDisplayModeInput(): # added by KB """ Switch display mode (binary, octal, decimal, hexadecimal) switchDisplayMode() should rotate through the options @@ -17,7 +20,8 @@ def switchDisplayModeInput(): #added by KB """ return str(input('Select a display mode - bin, oct, dec, hex: ')) -def trig_units_mode_input(): #added by KB + +def trig_units_mode_input(): # added by KB """ Switch trig units mode (Degrees, Radians) switchUnitsMode() should rotate through the options @@ -25,241 +29,485 @@ def trig_units_mode_input(): #added by KB """ return input('Select deg or rad: ') + def displayResult(temp_display: float): print("\nDISPLAY:\n") print(temp_display) -def performCalcLoop(calc, temp_display): # KB - removed none assignment to temp_display +def performCalcLoop(calc, temp_display): # KB - removed none assignment to temp_display + print('\nBOO! Happy Halloween ;) \n\n "Welcome to your Scientific Calculator."') print("\nHere's a list of choices:") - print('~' * 70) - print("1 : Addition \t\t 17 : Inverse sine") - print("2 : Subtraction \t 18 : Inverse cosine") - print("3 : Multiplication\t 19 : Inverse tangent") - print("4 : Division \t\t 20 : Trig units mode - Convert Radians to Degrees") - print("5 : Square \t 21 : Trig units mode - Convert Degrees to Radians") - print("6 : Square Root\t 22 : Factorial") - print("7 : Variable Exponentiation \t 23 : Log x") - print("8 : Inverse of Display \t 24 : 10 power x - Inverse nog") - print("9 : Invert Sign (+/-)\t 25 : Ln x - Natural log") - print("10 : Switch Display\t 26 : e power x - Inverse natural log") - print("11 : M+ \t 27 : Pi constant") - print("12 : MC \t 28 : e constant") - print("13 : MRC \t 29: Clear") - print("14 : Sine\t 30: Quit") - print("15 : Cosine") - print("16 : Tangent") - print('~' * 70) - + print('~' * 85) + print("1 : Addition \t\t 16 : Tangent") + print("2 : Subtraction \t 17 : Inverse sine") + print("3 : Multiplication\t 18 : Inverse cosine") + print("4 : Division \t\t 19 : Inverse tangent") + print("5 : Square \t 20 : Trig units mode - Convert Radians to Degrees") + print("6 : Square Root\t 21 : Trig units mode - Convert Degrees to Radians") + print("7 : Variable Exponentiation \t 22 : Factorial") + print("8 : Inverse of Display \t 23 : Log x") + print("9 : Invert Sign (+/-)\t 24 : 10 power x - Inverse nog") + print("10 : Switch Display\t 25 : Ln x - Natural log") + print("11 : M+ \t\t 26 : e power x - Inverse natural log") + print("12 : MC \t 27 : Pi") + print("13 : MRC \t 28 : Exponentiation constant") + print("14 : Sine\t 29 : Clear") + print("15 : Cosine 30 : Quit") + print('~' * 85) temp_display = 0 print("DISPLAY:") print(temp_display) memory = 0.0 - while True: - try: - choice = input("Enter number to choose your operation:\n") - - except: - print("Please enter a valid number.") + while temp_display == 0: - if choice == '30': - break + try: + choice = input("\nEnter number to choose your operation:\n") - elif choice == '1': - a, b = getTwoNumbers() - temp_display = calc.add(a, b) - displayResult(temp_display) + except: + print("Please enter a valid number.") - elif choice == '2': - a, b = getTwoNumbers() - temp_display = calc.sub(a, b) - displayResult(temp_display) - - elif choice == '3': - a, b = getTwoNumbers() - temp_display = calc.mult(a, b) - displayResult(temp_display) + if choice == '1': + a, b = getTwoNumbers() + temp_display = calc.add(a, b) + displayResult(temp_display) - elif choice == '4': - a, b = getTwoNumbers() - if b == 0: - temp_display = "Err" + elif choice == '2': + a, b = getTwoNumbers() + temp_display = calc.sub(a, b) displayResult(temp_display) - else: - temp_display = calc.div(a, b) + + elif choice == '3': + a, b = getTwoNumbers() + temp_display = calc.mult(a, b) displayResult(temp_display) - elif choice == '5': - a = getOneNumber() - temp_display = calc.sq(a) - displayResult(temp_display) + elif choice == '4': + a, b = getTwoNumbers() + if b == 0: + temp_display = "Err" + displayResult(temp_display) + else: + temp_display = calc.div(a, b) + displayResult(temp_display) + + elif choice == '5': + a = getOneNumber() + temp_display = calc.sq(a) + displayResult(temp_display) - elif choice == '6': - a = getOneNumber() - temp_display = calc.sqrt(a) - displayResult(temp_display) + elif choice == '6': + a = getOneNumber() + temp_display = calc.sqrt(a) + displayResult(temp_display) - elif choice == '7': - a, b = getTwoNumbers() - temp_display = calc.varexp(a, b) - displayResult(temp_display) + elif choice == '7': + a, b = getTwoNumbers() + temp_display = calc.varexp(a, b) + displayResult(temp_display) - elif choice == '8': - a = getOneNumber() - temp_display = calc.inverse(a) - displayResult(temp_display) + elif choice == '8': + a = getOneNumber() + temp_display = calc.inverse(a) + displayResult(temp_display) - elif choice == '9': - a = getOneNumber() - temp_display = calc.invert_sign(a) - displayResult(temp_display) + elif choice == '9': + a = getOneNumber() + temp_display = calc.invert_sign(a) + displayResult(temp_display) - elif choice == '10': #added by KB + elif choice == '10': # added by KB # switch display - a = switchDisplayModeInput() - displayResult(calc.switchDisplayMode(a,temp_display)) - - elif choice == '11': #added by KB - displayResult(calc.madd(temp_display,memory)) - memory = calc.madd(temp_display, memory) - #return memory - - elif choice == '12': #added by KB - displayResult(calc.mclear()) - memory = calc.mclear() - #return memory - - elif choice == '13': #added by KB - displayResult(memory) - #displayResult(calc.mrecall(memory)) - #memory = calc.mrecall(memory) - #return memory - - elif choice == '14': #added by KB - a = trig_units_mode_input() - x=getOneNumber() - if a == 'deg': - temp_display = calc.sin_deg(x) + a = switchDisplayModeInput() + displayResult(calc.switchDisplayMode(a, temp_display)) + + elif choice == '11': # added by KB + displayResult(calc.madd(temp_display, memory)) + memory = calc.madd(temp_display, memory) + # return memory + + elif choice == '12': # added by KB + displayResult(calc.mclear()) + memory = calc.mclear() + # return memory + + elif choice == '13': # added by KB + displayResult(memory) + # displayResult(calc.mrecall(memory)) + # memory = calc.mrecall(memory) + # return memory + + elif choice == '14': # added by KB + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.sin_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.sin_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + + elif choice == '15': # added by KB + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.cos_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.cos_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + + elif choice == '16': # added by KB + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.tan_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.tan_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + elif choice == '17': # added by KB + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.inv_sin_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.inv_sin_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + + elif choice == '18': # added by KB + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.inv_cos_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.inv_cos_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + + elif choice == '19': # added by KB + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.inv_tan_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.inv_tan_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + elif choice == '20': # added by KB + x = getOneNumber() + temp_display = calc.trig_units_mode_deg_to_rad(x) + print(displayResult(temp_display), " radians") + + elif choice == '21': # added by KB + x = getOneNumber() + temp_display = calc.trig_units_mode_rad_to_deg(x) + print(displayResult(temp_display), " degrees") + + elif choice == '22': # added by KB + x = getOneNumber() + temp_display = calc.factorial(x) + print(displayResult(temp_display)) + + elif choice == '23': # added by KB + print("Enter your number and base as prompted below\n") + x, b = getTwoNumbers() + temp_display = calc.log(x, b) + print(displayResult(temp_display)) + + elif choice == '24': # added by KB + x = getOneNumber() + temp_display = calc.inverse_log(x) + print(displayResult(temp_display)) + + elif choice == '25': # added by KB + x = getOneNumber() + temp_display = calc.ln(x) + print(displayResult(temp_display)) + + elif choice == '26': # added by KB + x = getOneNumber() + temp_display = calc.inv_ln(x) + print(displayResult(temp_display)) + + elif choice == '27': # added by KB + temp_display = calc.pi() displayResult(temp_display) - elif a == 'rad': - temp_display = calc.sin_rad(x) - displayResult(temp_display) - else: - print("That is not a valid input.") - - elif choice == '15': # added by KB - a = trig_units_mode_input() - x = getOneNumber() - if a == 'deg': - temp_display = calc.cos_deg(x) + elif choice == '28': # added by KB + temp_display = calc.e() displayResult(temp_display) - elif a == 'rad': - temp_display = calc.cos_rad(x) + + elif choice == '29': + temp_display = 0 displayResult(temp_display) + + elif choice == '30': + return + + elif temp_display == "Err": + if choice == '29': + temp_display = 0 + displayResult(temp_display) + + else: + temp_display = "Err" # TL Errors must be cleared before any other operation can take place + displayResult(temp_display) + else: print("That is not a valid input.") + displayResult(temp_display) + + while temp_display != 0: + + try: + choice = input("\nEnter number to choose your operation: \n") + + except: + print("Please enter a valid number.") + if temp_display == "Err": + if choice == '29': + temp_display = 0 + displayResult(temp_display) - elif choice == '16': # added by KB - a = trig_units_mode_input() - x = getOneNumber() - if a == 'deg': - temp_display = calc.tan_deg(x) + else: + temp_display = "Err" # TL Errors must be cleared before any other operation can take place + displayResult(temp_display) + + elif choice == '1': + b = getOneNumber() + temp_display = calc.add(temp_display, b) displayResult(temp_display) - elif a == 'rad': - temp_display = calc.tan_rad(x) + + elif choice == '2': + b = getOneNumber() + temp_display = calc.sub(temp_display, b) displayResult(temp_display) - else: - print("That is not a valid input.") - elif choice == '17': #added by KB - a = trig_units_mode_input() - x=getOneNumber() - if a == 'deg': - temp_display = calc.inv_sin_deg(x) + elif choice == '3': + b = getOneNumber() + temp_display = calc.mult(temp_display, b) displayResult(temp_display) - elif a == 'rad': - temp_display = calc.inv_sin_rad(x) + + elif choice == '4': + b = getOneNumber() + if b == 0: + print("\nDISPLAY:\nErr") + else: + temp_display = calc.div(temp_display, b) + displayResult(temp_display) + + elif choice == '5': + temp_display = calc.sq(temp_display) + displayResult(temp_display) + + elif choice == '6': + temp_display = calc.sqrt(temp_display) displayResult(temp_display) - else: - print("That is not a valid input.") + elif choice == '7': + b = getTwoNumbers() + temp_display = calc.varexp(temp_display, b) + displayResult(temp_display) - elif choice == '18': # added by KB - a = trig_units_mode_input() - x = getOneNumber() - if a == 'deg': - temp_display = calc.inv_cos_deg(x) + elif choice == '8': + temp_display = calc.inverse(temp_display) displayResult(temp_display) - elif a == 'rad': - temp_display = calc.inv_cos_rad(x) + + elif choice == '9': + temp_display = calc.invert_sign(temp_display) displayResult(temp_display) - else: - print("That is not a valid input.") + elif choice == '10': # added by KB + # switch display + a = switchDisplayModeInput() + displayResult(calc.switchDisplayMode(a, temp_display)) + + elif choice == '11': # added by KB + displayResult(calc.madd(temp_display, memory)) + memory = calc.madd(temp_display, memory) + # return memory + + elif choice == '12': # added by KB + displayResult(calc.mclear()) + memory = calc.mclear() + # return memory + + elif choice == '13': # added by KB + displayResult(memory) + # displayResult(calc.mrecall(memory)) + # memory = calc.mrecall(memory) + # return memory + + elif choice == '14': # added by KB + a = trig_units_mode_input() + # x=getOneNumber() + x = temp_display + if a == 'deg': + temp_display = calc.sin_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.sin_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + + elif choice == '15': # added by KB + a = trig_units_mode_input() + x = temp_display + # x = getOneNumber() + if a == 'deg': + temp_display = calc.cos_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.cos_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + + elif choice == '16': # added by KB + a = trig_units_mode_input() + x = temp_display + # x = getOneNumber() + if a == 'deg': + temp_display = calc.tan_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.tan_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + elif choice == '17': # added by KB + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.inv_sin_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.inv_sin_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + + elif choice == '18': # added by KB + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.inv_cos_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.inv_cos_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + + elif choice == '19': # added by KB + a = trig_units_mode_input() + x = getOneNumber() + if a == 'deg': + temp_display = calc.inv_tan_deg(x) + displayResult(temp_display) + elif a == 'rad': + temp_display = calc.inv_tan_rad(x) + displayResult(temp_display) + else: + print("That is not a valid input.") + + elif choice == '20': # added by KB + x = getOneNumber() + temp_display = calc.trig_units_mode_deg_to_rad(x) + print(displayResult(temp_display), " radians") + + elif choice == '21': # added by KB + x = getOneNumber() + temp_display = calc.trig_units_mode_rad_to_deg(x) + print(displayResult(temp_display), " degrees") + + elif choice == '22': # added by KB + x = getOneNumber() + temp_display = calc.factorial(x) + print(displayResult(temp_display)) + + elif choice == '23': # added by KB + print("Enter your number and base as prompted below\n") + x, b = getTwoNumbers() + temp_display = calc.log(x, b) + print(displayResult(temp_display)) + + elif choice == '24': # added by KB + x = getOneNumber() + temp_display = calc.inverse_log(x) + print(displayResult(temp_display)) + + elif choice == '25': # added by KB + x = getOneNumber() + temp_display = calc.ln(x) + print(displayResult(temp_display)) + + elif choice == '26': # added by KB + x = getOneNumber() + temp_display = calc.inv_ln(x) + print(displayResult(temp_display)) + + elif choice == '27': # added by KB + temp_display = calc.pi() + displayResult(temp_display) - elif choice == '19': # added by KB - a = trig_units_mode_input() - x = getOneNumber() - if a == 'deg': - temp_display = calc.inv_tan_deg(x) + elif choice == '28': # added by KB + temp_display = calc.e() displayResult(temp_display) - elif a == 'rad': - temp_display = calc.inv_tan_rad(x) + + elif choice == '29': + temp_display = 0 displayResult(temp_display) + + elif choice == '30': + return + + elif temp_display == "Err": + if choice == '29': + temp_display = 0 + displayResult(temp_display) + + else: + temp_display = "Err" # TL Errors must be cleared before any other operation can take place + displayResult(temp_display) + else: print("That is not a valid input.") + displayResult(temp_display) - elif choice == '20': # added by KB - x = getOneNumber() - temp_display = calc.trig_units_mode_deg_to_rad(x) - print(displayResult(temp_display)," radians") - - elif choice == '21': # added by KB - x = getOneNumber() - temp_display = calc.trig_units_mode_rad_to_deg(x) - print(displayResult(temp_display), " degrees") - - elif choice == '22':# added by KB - x = getOneNumber() - temp_display = calc.factorial(x) - displayResult(temp_display) - - elif choice =='23':# added by KB - print("Enter your number and base as prompted below\n") - x,b = getTwoNumbers() - temp_display = calc.log(x,b) - print(displayResult(temp_display)) - - elif choice == '24':# added by KB - x = getOneNumber() - temp_display = calc.inverse_log(x) - print(displayResult(temp_display)) - - elif choice == '25':# added by KB - x = getOneNumber() - temp_display = calc.ln(x) - print(displayResult(temp_display)) - - elif choice == '26':# added by KB - x = getOneNumber() - temp_display = calc.inv_ln(x) - print(displayResult(temp_display)) - - elif choice == '27':# added by KB - temp_display = calc.pi() - displayResult(temp_display) - - elif choice == '28':# added by KB - temp_display = calc.e() - displayResult(temp_display) # main start