diff --git a/calculator.py b/calculator.py index 3c85ead..e35b1b4 100644 --- a/calculator.py +++ b/calculator.py @@ -7,6 +7,32 @@ 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 + + # add lots more methods to this calculator class. 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..39efc04 --- /dev/null +++ b/functionslafferty.py @@ -0,0 +1,69 @@ +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 == None: + + print("Display: " + 0) + + display = Tk() + root = Tk() + root.geometry("312x324") + root.resizable(0,0) + root.title("CALCULATOR") + + + if answer == 'quit': + break + result(answer) + +intro() \ No newline at end of file diff --git a/main-app-dev.py b/main-app-dev.py new file mode 100644 index 0000000..9eabbeb --- /dev/null +++ b/main-app-dev.py @@ -0,0 +1,180 @@ +from calculator import Calculator + + +def getTwoNumbers(): + a = float(input("First Number? ")) + b = float(input("Second Number? ")) + return a, b + + +def getOneNumber(): # get one number instead + a = float(input("Number? ")) + return a + + +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 : ") + 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 : Clear") + print("11 : \t\t 22 : ") + print('~' * 70) + temp_display = 0 + print("DISPLAY:") + print(temp_display) + + while True: + 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: + 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 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 == '2': + b = getOneNumber() + temp_display = calc.sub(temp_display, b) + displayResult(temp_display) + + elif choice == '3': + b = getOneNumber() + temp_display = calc.mult(temp_display, b) + 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 == '5': + temp_display = calc.sq(temp_display) + displayResult(temp_display) + + 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, 0) #KB - passed second input parameter 0 + 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():