Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Enhancing below: from MyLib import * cont = y #looping while cont == 'y' : #exeption to check input try : lr = int(input( Enter
Enhancing below:
from MyLib import * cont = "y" #looping while cont == 'y': #exeption to check input try: lr = int(input("Enter your Lower Range: ")) hr = int(input("Enter your higher Range: ")) num1 = float(input("Enter the first number: ")) num2 = float(input("Enter the second number: ")) print() if IsInRange(lr, hr, num1, num2): print("The result of ", num1, "+", num2, "=", add(num1, num2)) print("The result of ", num1, "-", num2, "=", sub(num1, num2)) print("The result of ", num1, "*", num2, "=", mult(num1, num2)) print("The result of ", num1, "/", num2, "=", div(num1, num2)) print() cont = input("Continue Looping (y/n) :").lower() if cont == 'n': print("Thank you for using my calculator!") break except: print('Please enter valid number.')
MyLib
#functions for math operations def add(num1, num2): add = num1 + num2 return add def sub(num1, num2): sub = num1 - num2 return sub #add exception for zero def div(num1, num2): try: div = num1 / num2 return div except ZeroDivisionError: return 'Cannot divide by zero' def mult(num1, num2): mult = num1 * num2 return mult #Are the numbers in range? check with function. return true if so def IsInRange(lr, hr, num1, num2): if lr < num1 < hr and lr < num2 < hr: return True else: print("The input values are outside the input ranges Please check the number and try again. Thank You for using our calculator.") def scalc(string): values = string.split(",") operation = values[-1] first_value = float(values[0]) second_value = float(values[1]) if(operation == '+'): result = add(first_value, second_value) elif(operation == "-"): result = sub(first_value, second_value) elif(operation == "/"): result = div(first_value, second_value) elif(operation == "*"): result = mult(first_value, second_value) else: # treat wrong cases result = None return result
How to:
1) Use list to make a menu
2) Make a function the will return the results of the four operations in a dictionary allInOne(n1,n2)
Sample output
1)Add two numbers
2)Mult two number
3)Divide
4)Scalc
5)all in one..
6)...
res=allInOne(5,2)
The results will be return in this format;
res is dictionary {"add":7, "sub":3, "mult":10, "div":2.5)
from res, you are going to print
5 + 2 = 7
5 - 2 = 3
5 * 2 = 10
5 / 2 = 2.5
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started