Feb-24-2020, 02:21 AM
I'm getting a syntax error in my program and I can't seem to get it fixed. It could be a simple indent issue but I have tried that. I am a newbie so it is probably something simple I've been racking my brain with this any help would be appreciated.
#Mylib class calculatorClass: def add(self,a, b): return a + b # This function subtracts two numbers def subtract(self,a, b): return a - b # This function multiplies two numbers def multiply(self,a, b): return a * b # This function divides two numbers # The ZeroDivisionError exception is raised when division or modulo by zero takes place for all numeric types def divide(self,a, b): try: return a / b # This exception will be raised when the user attempts division by zero # Implemented here so if the users input is '0' it will display error that you can't divide by zero except ZeroDivisionError: print("Error: Cannot divide by zero!") # This function allows for string manipulation def scalc(self,p1): string1 = p1.split(",") if string1[2] == "+": result = addition(float(string1[0]), float(string1[1])) elif string1[2] == "-": result = subtraction(float(string1[0]), float(string1[1])) elif string1[2] == "*": result = multiplication(float(string1[0]), float(string1[1])) elif string1[2] == "/": result = division(float(string1[0]), float(string1[1])) return result # This function returns the results of input values in addition, subtraction, multiplication and division def AllInOne(self,a, b): add = a + b subtract = a - b multiply = a * b divide = a / b print('res is dictionary', {"add": add, "sub": subtract, "mult": multiply, "div": divide}) return{"add": add, "sub": subtract, "mult": multiply, "div": divide}
from Mylib import add, subtract, multiply, divide, AllInOne, scalc def main(): print("\nWelcome to Calculator!\n") calculatorObj = calculatorClass() menuList = ["1) Addition", "2) Subtraction", "3) Multiplication", "4) Division", "5) Scalc", "6) AllInOne", "0) Exit\n"] # Takes user input to proceed with selected math operations while True: try: print("Please enter your choice to perform a math operation: ") for n in menuList: print(n) menuChoice = int(input("Please input your selection then press ENTER: ")) if menuChoice == 0: print("Goodbye!") return # Checks users input against available options in menu list, if not user is notified to try again. if menuChoice > 6 or menuChoice < 0: print("\nNot a valid selection from the menu!" "\nTry again! ") main() # This exception will be raised if the users input is not the valid data expected in this argument. except ValueError: print("\nError!! Only integer values, Please try again!") main() # User input to determine input of range a user can work with print("Please enter a value for low & high range between -100 & 100") lowRange = int(input("Enter your low range: ")) if lowRange < -100 or lowRange > 100: print("Error: ensure input doesn't exceed range of -100 to 100") break highRange = int(input("Enter your high range: ")) if highRange < -100 or highRange > 100: print("Error: ensure input doesn't exceed range of -100 to 100") break # Takes user input to perform math operations with input value firstNum = float(input("Enter your first number: ")) secondNum = float(input("Enter your second number: ")) # Checks users input value is within the given ranges while True: if firstNum < lowRange or firstNum > highRange: print("Error: first number input exceeds ranges, try again!") break if secondNum < lowRange or secondNum > highRange: print("Error: second number input exceeds ranges, try again!") break # Computes math calculations based on users menu selection if menuChoice == 1: print(firstNum, '+', secondNum, '=', calculatorObj.addition(firstNum, secondNum)) elif menuChoice == 2: print(firstNum, '-', secondNum, '=', calculatorObj.subtraction(firstNum, secondNum)) elif menuChoice == 3: print(firstNum, '*', secondNum, '=', calculatorObj.multiplication(firstNum, secondNum)) elif menuChoice == 4: print(firstNum, '/', secondNum, '=', calculatorObj.division(firstNum, secondNum)) elif menuChoice == 5: print('The result of ', firstNum, '+', secondNum, '=', calculatorObj.scalc(str(firstNum) + ',' + str(secondNum) + ',+')) print('The result of ', firstNum, '-', secondNum, '=', calculatorObj.scalc(str(firstNum) + ' ,' + str(secondNum) + ',-')) print('The result of ', firstNum, '*', secondNum, '=', calculatorObj.scalc(str(firstNum) + ', ' + str(secondNum) + ',*')) print('The result of ', firstNum, '/', secondNum, '=', calculatorObj.scalc(str(firstNum) + ', ' + str(secondNum) + ',/')) elif menuChoice == 6: res = calculatorObj.allInOne(firstNum, secondNum) print(str(firstNum) + " + " + str(secondNum) + " = " + str(res["add"])) print(str(firstNum) + " - " + str(secondNum) + " = " + str(res["sub"])) print(str(firstNum) + " * " + str(secondNum) + " = " + str(res["mult"])) print(str(firstNum) + " / " + str(secondNum) + " = " + str(res["div"])) # This will ask the user if they want to us the calculator again while True: reCalculate = input("\nWould you like to try another operation? (y/n): ") if reCalculate == 'Y' or reCalculate == 'y': print() main() else: print("\nThanks for using calculator! " "\nGoodbye!") return main()