Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Good Morning, In Python enhance the following code by 1) Add in current menu items into a seperate file that you can write to and

Good Morning,

In Python enhance the following code by

1) Add in current menu items into a seperate file that you can write to and read from a file

2) create a class wrfile() with two methods to write the results to a file, and to read the results from a file.

3) operation result should write into file

class calculatorClass: def addition(self, a, b): return a + b

# This subtracts two numbers def subtraction(self, a, b): return a - b

# This function multiplies two numbers def multiplication(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 division(self, a, b): try: a / b # Exception raised when user attempts division 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 = self.addition(float(string1[0]), float(string1[1])) elif string1[2] == "-": result = self.subtraction(float(string1[0]), float(string1[1])) elif string1[2] == "*": result = self.multiplication(float(string1[0]), float(string1[1])) elif string1[2] == "/": result = self.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}

# This is the main function, this is the starting point of execution of the program def main(): print(" Welcome to Calculator! ") calcMath = calculatorClass() # Menu of available math operations for use menuList = ["1) Addition", "2) Subtraction", "3) Multiplication", "4) Division", "5) Scalc", "6) All in one", "0) Exit "] # 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(" Not a valid selection from the menu!" " Try again! ") main() # This exception will be raised if the users input is not the valid data expected in this argument. except ValueError: print(" Error!! 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, '=', calcMath.scalc(firstNum, secondNum)) elif menuChoice == 2: print(firstNum, '-', secondNum, '=', calcMath.scalc(firstNum, secondNum)) elif menuChoice == 3: print(firstNum, '*', secondNum, '=', calcMath.scalc(firstNum, secondNum)) elif menuChoice == 4: print(firstNum, '/', secondNum, '=', calcMath.scalc(firstNum, secondNum)) elif menuChoice == 5: print('The result of ', firstNum, '+', secondNum, '=', calcMath.scalc(str(firstNum) + ',' + str(secondNum) + ',+')) print('The result of ', firstNum, '-', secondNum, '=', calcMath.scalc(str(firstNum) + ' ,' + str(secondNum) + ',-')) print('The result of ', firstNum, '*', secondNum, '=', calcMath.scalc(str(firstNum) + ', ' + str(secondNum) + ',*')) print('The result of ', firstNum, '/', secondNum, '=', calcMath.scalc(str(firstNum) + ', ' + str(secondNum) + ',/')) elif menuChoice == 6: res = calcMath.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(" Would you like to try another operation? (y/n): ") if reCalculate == 'Y' or reCalculate == 'y': print() main() else: print(" Thanks for using this calculator! " " Goodbye!") return

main()

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Understanding Groups

Answered: 1 week ago