Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, In Python Enhance the prior assignment by doing the following 1) Create a class that contain all prior functions MyLib # This function adds

Hello,

In Python

Enhance the prior assignment by doing the following

1) Create a class that contain all prior functions

MyLib

# This function adds two numbers def addition(a, b): return a + b # This function subtracts two numbers def subtraction(a, b): return a - b # This function multiplies two numbers def multiplication(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(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(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(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 module contains math operation functions import MyLib # This is the main function, this is the starting point of execution of the program def main(): print(" Welcome to Calculator! ") # 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!") sys.exit() # 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, '=', MyLib.addition(firstNum, secondNum)) elif menuChoice == 2: print(firstNum, '-', secondNum, '=', MyLib.subtraction(firstNum, secondNum)) elif menuChoice == 3: print(firstNum, '*', secondNum, '=', MyLib.multiplication(firstNum, secondNum)) elif menuChoice == 4: print(firstNum, '/', secondNum, '=', MyLib.division(firstNum, secondNum)) elif menuChoice == 5: print('The result of ', firstNum, '+', secondNum, '=', MyLib.scalc(str(firstNum) + ',' + str(secondNum) + ',+')) print('The result of ', firstNum, '-', secondNum, '=', MyLib.scalc(str(firstNum) + ' ,' + str(secondNum) + ',-')) print('The result of ', firstNum, '*', secondNum, '=', MyLib.scalc(str(firstNum) + ', ' + str(secondNum) + ',*')) print('The result of ', firstNum, '/', secondNum, '=', MyLib.scalc(str(firstNum) + ', ' + str(secondNum) + ',/')) elif menuChoice == 6: res = MyLib.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 calculator! " " Goodbye!") sys.exit() main() 

Thank you!

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

Database Technology And Management Computers And Information Processing Systems For Business

Authors: Robert C. Goldstein

1st Edition

0471887374, 978-0471887379

More Books

Students also viewed these Databases questions

Question

Perform an Internet search. Discuss a company that uses EPLI.

Answered: 1 week ago

Question

How do you feel about employment-at-will policies? Are they fair?

Answered: 1 week ago