From 74ea349245a9ff55c4bb0b3a8c269e2717edbc4b Mon Sep 17 00:00:00 2001 From: TomLafferty Date: Sat, 30 Oct 2021 00:39:52 -0400 Subject: [PATCH 01/10] functioning base calculator with input and functions from math. --- dev.py | 17 ++++++++++++++ functionslafferty.py | 53 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 dev.py create mode 100644 functionslafferty.py diff --git a/dev.py b/dev.py new file mode 100644 index 0000000..9ce70db --- /dev/null +++ b/dev.py @@ -0,0 +1,17 @@ +import math + + +class Calculator: + + def __init__(self): + pass + + def add(self, x, y): + return x + y + + def sub(self, x, y): + return 0 + + +# add lots more methods to this calculator class. + diff --git a/functionslafferty.py b/functionslafferty.py new file mode 100644 index 0000000..5606d18 --- /dev/null +++ b/functionslafferty.py @@ -0,0 +1,53 @@ +import math + +def calc(term): + """ + The term will be the input from the calculator. + Output will be result of computed term. + This is where the functions will actually calculate. + """ + + term = term.replace(' ','') # gets rid of spaces + term = term.replace('^','**') # to power of sign recognized as ** + term = term.replace('=','') + term = term.replace('?','') + term = term.replace('%','/100') # for percentages + term = term.replace('rad','radians') + term = term.replace('mod','%') # for modula + + functions = ['sin', 'cos', 'tan', 'cosh', 'sinh', 'tanh', 'sqrt', 'pi', 'radians', 'e', 'square', 'exp','factorial'] + + term = term.lower() # convert to lowercase for case sensitive + + for function in functions: + if function in term: + usemath = 'math.' + function # will source the function and combine with math. to create math function + term = term.replace(function , usemath) + + try: + term = eval(term) + + except ZeroDivisionError: # builds in zero division error + print("Sorry it is not possible to divide by zero! Try again my friend.") + except NameError: + print("Sorry that input is not valid! Try again my friend.") + except AttributeError: + print("Incorrect usage method! Try again my friend.") + return(term) + +def result(term): # This passes the argument 'term' to the function 'calc' and prints the result + + print("\n" + str(calc(term))) + +def intro(): + """This is for handling the user data and printing the introduction and input + """ + while True: + print("\n BOO!! Welcome to the Halloween Scientific Calculator!! Input in any format, enter 'quit' to escape!!") + answer = input("\n What do you want to know? ") + + if answer == 'quit': + break + result(answer) + +intro() \ No newline at end of file From 9d8950e196aab96a3af304bd926c6be3b447f5f6 Mon Sep 17 00:00:00 2001 From: TomLafferty Date: Sat, 30 Oct 2021 17:07:20 -0400 Subject: [PATCH 02/10] basic functions input --- functionslafferty.py | 16 +++++++++ main-app-dev.py | 80 ++++++++++++++++++++++++++++++++++++++++++++ main-app.py | 1 + 3 files changed, 97 insertions(+) create mode 100644 main-app-dev.py diff --git a/functionslafferty.py b/functionslafferty.py index 5606d18..39efc04 100644 --- a/functionslafferty.py +++ b/functionslafferty.py @@ -1,5 +1,6 @@ import math + def calc(term): """ The term will be the input from the calculator. @@ -39,13 +40,28 @@ def result(term): # This passes the argument 'term' to the function 'calc' and print("\n" + str(calc(term))) + def intro(): """This is for handling the user data and printing the introduction and input """ + while True: + print("\n BOO!! Welcome to the Halloween Scientific Calculator!! Input in any format, enter 'quit' to escape!!") + answer = input("\n What do you want to know? ") + if answer == None: + + print("Display: " + 0) + + display = Tk() + root = Tk() + root.geometry("312x324") + root.resizable(0,0) + root.title("CALCULATOR") + + if answer == 'quit': break result(answer) diff --git a/main-app-dev.py b/main-app-dev.py new file mode 100644 index 0000000..a005704 --- /dev/null +++ b/main-app-dev.py @@ -0,0 +1,80 @@ +from calculator import Calculator +import math + + +def getTwoNumbers(): + a = float(input("first number? ")) + b = float(input("second number? ")) + return a, b + + +def getOneNumber(): + a = float(input("first number? ")) + return a + + +def displayResult(x: float): + print(x, "\n") + + +def performCalcLoop(calc): + while True: + choice = input("Operation? ") + if choice == 'q': + print("Thanks for stopping by, have a great day.") + break # user types q to quit calulator. + + elif choice == 'add': + a, b = getTwoNumbers() + displayResult(calc.add(a, b)) + + elif choice == 'subtract': + a, b = getTwoNumbers() + displayResult(a-b) + + elif choice == 'multiply': + a, b = getTwoNumbers() + displayResult(a*b) + + elif choice == 'divide': + a, b = getTwoNumbers() + if b == 0: + print("Err") + else: + displayResult(a/b) + +# Have to use getOneNumber for certain operations + + elif choice == 'square': + a = getOneNumber() + displayResult(a**2) + + elif choice == 'square root': + a = getOneNumber() + displayResult(math.sqrt(a)) + + elif choice == 'variable exponentiation': + a, b = getTwoNumbers() + displayResult(a**b) + + elif choice == 'inverse': + a = getOneNumbers() + displayResult(1 / a) + + elif choice == 'invert sign': + a = getOneNumber() + displayResult(a * -1) + + else: + print("That is not a valid input.") + + +# main start +def main(): + calc = Calculator() + performCalcLoop(calc) + print("Done Calculating.") + + +if __name__ == '__main__': + main() diff --git a/main-app.py b/main-app.py index a7cc4e2..6da8c07 100644 --- a/main-app.py +++ b/main-app.py @@ -1,4 +1,5 @@ from calculator import Calculator +import math def getTwoNumbers(): From 8c2ccc896e769601b35097927e253485bf8759e0 Mon Sep 17 00:00:00 2001 From: TomLafferty Date: Sat, 30 Oct 2021 17:44:15 -0400 Subject: [PATCH 03/10] with calc class --- calculator.py | 29 ++++++++++++++++++++++++++++- main-app-dev.py | 20 +++++++++----------- 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/calculator.py b/calculator.py index 3c85ead..3bda1e6 100644 --- a/calculator.py +++ b/calculator.py @@ -1,3 +1,5 @@ +import * from math + class Calculator: def __init__(self): @@ -7,6 +9,31 @@ 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): + return x / y + + def sq(self, x): + return x**2 + + def sqrt(self, x): + return sqrt(x) + + def varexp(self, x, y): + return x**y + + def inverve(self, x): + return 1 / x + + def invert_sign(self, x): + return x * -1 + + def sub(self, x, y): + return a * b + # add lots more methods to this calculator class. diff --git a/main-app-dev.py b/main-app-dev.py index a005704..86d777d 100644 --- a/main-app-dev.py +++ b/main-app-dev.py @@ -30,40 +30,38 @@ def performCalcLoop(calc): elif choice == 'subtract': a, b = getTwoNumbers() - displayResult(a-b) + displayResult(calc.sub(a, b)) elif choice == 'multiply': a, b = getTwoNumbers() - displayResult(a*b) + displayResult(calc.mult(a, b)) elif choice == 'divide': a, b = getTwoNumbers() if b == 0: print("Err") else: - displayResult(a/b) + displayResult(calc.div(a, b) -# Have to use getOneNumber for certain operations - - elif choice == 'square': + elif choice == 'square': # Have to use getOneNumber for certain operations a = getOneNumber() - displayResult(a**2) + displayResult(calc.sq(a)) elif choice == 'square root': a = getOneNumber() - displayResult(math.sqrt(a)) + displayResult(calc.sqrt(a)) elif choice == 'variable exponentiation': a, b = getTwoNumbers() - displayResult(a**b) + displayResult(calc.varexp(a, b)) elif choice == 'inverse': a = getOneNumbers() - displayResult(1 / a) + displayResult(calc.inverse(a)) elif choice == 'invert sign': a = getOneNumber() - displayResult(a * -1) + displayResult(calc.invert_sign(a)) else: print("That is not a valid input.") From e0aad94ac75ea8a2d8e5ac5999a0f73f74064b81 Mon Sep 17 00:00:00 2001 From: TomLafferty Date: Sat, 30 Oct 2021 17:57:56 -0400 Subject: [PATCH 04/10] corrected --- calculator.py | 4 +--- main-app-dev.py | 7 +++---- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/calculator.py b/calculator.py index 3bda1e6..289e0d3 100644 --- a/calculator.py +++ b/calculator.py @@ -1,5 +1,3 @@ -import * from math - class Calculator: def __init__(self): @@ -21,7 +19,7 @@ def sq(self, x): return x**2 def sqrt(self, x): - return sqrt(x) + return x*x def varexp(self, x, y): return x**y diff --git a/main-app-dev.py b/main-app-dev.py index 86d777d..2f35803 100644 --- a/main-app-dev.py +++ b/main-app-dev.py @@ -1,5 +1,4 @@ from calculator import Calculator -import math def getTwoNumbers(): @@ -8,7 +7,7 @@ def getTwoNumbers(): return a, b -def getOneNumber(): +def getOneNumber(): # get one number instead a = float(input("first number? ")) return a @@ -41,9 +40,9 @@ def performCalcLoop(calc): if b == 0: print("Err") else: - displayResult(calc.div(a, b) + displayResult(calc.div(a, b)) - elif choice == 'square': # Have to use getOneNumber for certain operations + elif choice == 'square': a = getOneNumber() displayResult(calc.sq(a)) From 3c434c1bdda41253e3d6df77b3d6526e271c014e Mon Sep 17 00:00:00 2001 From: TomLafferty Date: Sat, 30 Oct 2021 18:26:48 -0400 Subject: [PATCH 05/10] corrected again --- calculator.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/calculator.py b/calculator.py index 289e0d3..16afb18 100644 --- a/calculator.py +++ b/calculator.py @@ -30,8 +30,5 @@ def inverve(self, x): def invert_sign(self, x): return x * -1 - def sub(self, x, y): - return a * b - # add lots more methods to this calculator class. From 03e9b5d8e1d7cc5ae81716543b1bb296eba9b154 Mon Sep 17 00:00:00 2001 From: TomLafferty Date: Sun, 31 Oct 2021 14:58:22 -0400 Subject: [PATCH 06/10] input and temp display added --- calculator.py | 7 ++- main-app-dev.py | 128 +++++++++++++++++++++++++++++++----------------- 2 files changed, 87 insertions(+), 48 deletions(-) diff --git a/calculator.py b/calculator.py index 16afb18..9a4a3fa 100644 --- a/calculator.py +++ b/calculator.py @@ -13,18 +13,21 @@ 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*x + return x**(1/2) def varexp(self, x, y): return x**y - def inverve(self, x): + def inverse(self, x): return 1 / x def invert_sign(self, x): diff --git a/main-app-dev.py b/main-app-dev.py index 2f35803..db65c47 100644 --- a/main-app-dev.py +++ b/main-app-dev.py @@ -12,58 +12,94 @@ def getOneNumber(): # get one number instead return a -def displayResult(x: float): - print(x, "\n") +def displayResult(temp_display: float): + print("DISPLAY:\n") + print(temp_display) def performCalcLoop(calc): - while True: - choice = input("Operation? ") - if choice == 'q': - print("Thanks for stopping by, have a great day.") - break # user types q to quit calulator. - - elif choice == 'add': - a, b = getTwoNumbers() - displayResult(calc.add(a, b)) - - elif choice == 'subtract': - a, b = getTwoNumbers() - displayResult(calc.sub(a, b)) - - elif choice == 'multiply': - a, b = getTwoNumbers() - displayResult(calc.mult(a, b)) - - elif choice == 'divide': - a, b = getTwoNumbers() - if b == 0: - print("Err") - else: - displayResult(calc.div(a, b)) - - elif choice == 'square': - a = getOneNumber() - displayResult(calc.sq(a)) - - elif choice == 'square root': - a = getOneNumber() - displayResult(calc.sqrt(a)) - elif choice == 'variable exponentiation': - a, b = getTwoNumbers() - displayResult(calc.varexp(a, b)) - elif choice == 'inverse': - a = getOneNumbers() - displayResult(calc.inverse(a)) + 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 : ") + print("2 : Subtraction \t 13 : ") + print("3 : Multiplication\t 14 : ") + print("4 : Division \t\t 15 : ") + print("5 : Square \t 16 : ") + print("6 : Square Root\t 17 : ") + print("7 : Variable Exponentiation \t 18 : ") + print("8 : Inverse of Display \t 19 : ") + print("9 : Invert Sign (+/-)\t 20 : Quit") + print("10 : \t 21 : ") + print("11 : \t\t 22 : ") + print('~' * 70) + temp_display = 0 + print("DISPLAY:") + print(temp_display) + + while True: + try: + + choice = input("\nEnter 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 == '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("Err") + 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() + temp_display = calc.varexp(a, b) + displayResult(temp_display) + + elif choice == '8': + a = getOneNumbers() + temp_display = calc.inverse(a) + displayResult(temp_display) + + elif choice == '9': + a = getOneNumber() + temp_display = calc.invert_sign(a) + displayResult(temp_display) - elif choice == 'invert sign': - a = getOneNumber() - displayResult(calc.invert_sign(a)) - - else: - print("That is not a valid input.") + else: + print("That is not a valid input.") # main start From 84bc9bf91e6bf67675472b06424328b1a81e85ab Mon Sep 17 00:00:00 2001 From: TomLafferty Date: Sun, 31 Oct 2021 15:12:20 -0400 Subject: [PATCH 07/10] fixed typo --- main-app-dev.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main-app-dev.py b/main-app-dev.py index db65c47..d018f34 100644 --- a/main-app-dev.py +++ b/main-app-dev.py @@ -89,7 +89,7 @@ def performCalcLoop(calc): displayResult(temp_display) elif choice == '8': - a = getOneNumbers() + a = getOneNumber() temp_display = calc.inverse(a) displayResult(temp_display) From 3c8a282754a7a608cae9d0ed988582ad1cce9563 Mon Sep 17 00:00:00 2001 From: TomLafferty Date: Sun, 31 Oct 2021 15:15:11 -0400 Subject: [PATCH 08/10] added spacing before each display result --- main-app-dev.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main-app-dev.py b/main-app-dev.py index d018f34..1c9e584 100644 --- a/main-app-dev.py +++ b/main-app-dev.py @@ -13,7 +13,7 @@ def getOneNumber(): # get one number instead def displayResult(temp_display: float): - print("DISPLAY:\n") + print("\nDISPLAY:\n") print(temp_display) From cec6d3cbc30afa9842a59186f9eec7659501260f Mon Sep 17 00:00:00 2001 From: TomLafferty Date: Sun, 31 Oct 2021 15:23:27 -0400 Subject: [PATCH 09/10] minor fixes --- main-app-dev.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main-app-dev.py b/main-app-dev.py index 1c9e584..d4ec0a6 100644 --- a/main-app-dev.py +++ b/main-app-dev.py @@ -68,7 +68,7 @@ def performCalcLoop(calc): elif choice == '4': a, b = getTwoNumbers() if b == 0: - print("Err") + print("\nDISPLAY:\nErr") else: temp_display = calc.div(a, b) displayResult(temp_display) From ba8917812cc4d3e03b3d63614268b1540b2bbf08 Mon Sep 17 00:00:00 2001 From: TomLafferty Date: Sun, 31 Oct 2021 16:16:51 -0400 Subject: [PATCH 10/10] updated to pull from display when display not 0 --- calculator.py | 1 + main-app-dev.py | 183 +++++++++++++++++++++++++++++++++--------------- 2 files changed, 126 insertions(+), 58 deletions(-) diff --git a/calculator.py b/calculator.py index 9a4a3fa..e35b1b4 100644 --- a/calculator.py +++ b/calculator.py @@ -34,4 +34,5 @@ def invert_sign(self, x): return x * -1 + # add lots more methods to this calculator class. diff --git a/main-app-dev.py b/main-app-dev.py index d4ec0a6..9eabbeb 100644 --- a/main-app-dev.py +++ b/main-app-dev.py @@ -2,13 +2,13 @@ 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 - a = float(input("first number? ")) + a = float(input("Number? ")) return a @@ -17,7 +17,7 @@ def displayResult(temp_display: float): print(temp_display) -def performCalcLoop(calc): +def performCalcLoop(calc,temp_display): # KB - removed none assignment to temp_display print('\nBOO! Happy Halloween ;) \n\n "Welcome to your Scientific Calculator."') @@ -32,7 +32,7 @@ def performCalcLoop(calc): print("7 : Variable Exponentiation \t 18 : ") print("8 : Inverse of Display \t 19 : ") print("9 : Invert Sign (+/-)\t 20 : Quit") - print("10 : \t 21 : ") + print("10 : \t 21 : Clear") print("11 : \t\t 22 : ") print('~' * 70) temp_display = 0 @@ -40,72 +40,139 @@ def performCalcLoop(calc): print(temp_display) while True: - try: - - choice = input("\nEnter 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 == '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") + while temp_display == 0: + + try: + choice = input("\nEnter 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 + + 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() + 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 == '21': + #displayResult(temp_display) + else: - temp_display = calc.div(a, b) + print("That is not a valid input.") displayResult(temp_display) - elif choice == '5': - a = getOneNumber() - temp_display = calc.sq(a) - 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 choice == '20': + print("Thanks for stopping by, have a great day.") + break # user types q to quit calulator. + + elif choice == '1': + b = getOneNumber() + temp_display = calc.add(temp_display, b) + displayResult(temp_display) - elif choice == '6': - a = getOneNumber() - temp_display = calc.sqrt(a) - displayResult(temp_display) + elif choice == '2': + b = getOneNumber() + temp_display = calc.sub(temp_display, b) + displayResult(temp_display) - elif choice == '7': - a, b = getTwoNumbers() - temp_display = calc.varexp(a, b) - displayResult(temp_display) + elif choice == '3': + b = getOneNumber() + temp_display = calc.mult(temp_display, b) + displayResult(temp_display) - elif choice == '8': - a = getOneNumber() - temp_display = calc.inverse(a) - displayResult(temp_display) + elif choice == '4': + b = getOneNumber() + if b == 0: + print("\nDISPLAY:\nErr") + else: + temp_display = calc.div(temp_display, b) + displayResult(temp_display) - elif choice == '9': - a = getOneNumber() - temp_display = calc.invert_sign(a) - displayResult(temp_display) + elif choice == '5': + temp_display = calc.sq(temp_display) + displayResult(temp_display) - else: - print("That is not a valid input.") + elif choice == '6': + temp_display = calc.sqrt(temp_display) + displayResult(temp_display) + elif choice == '7': + b = getTwoNumbers() + temp_display = calc.varexp(temp_display, b) + displayResult(temp_display) + + elif choice == '8': + temp_display = calc.inverse(temp_display) + displayResult(temp_display) + + elif choice == '9': + temp_display = calc.invert_sign(temp_display) + displayResult(temp_display) + + elif choice == '21': + temp_display = 0 + displayResult(temp_display) + + else: + print("That is not a valid input.") + displayResult(temp_display) # main start def main(): calc = Calculator() - performCalcLoop(calc) + performCalcLoop(calc, 0) #KB - passed second input parameter 0 print("Done Calculating.")