Question: Hello! I am having trouble working through this task and would appreciate help working through it. Thank you! You are going to enhance the prior
Hello! I am having trouble working through this task and would appreciate help working through it. Thank you!
You are going to enhance the prior assignment by doing the following
1) Use list to create a menu
2) Create 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
My prior completed assignment is below.
#Import module named Mylib
import Mylib
#User inputs lower range, higher range and first and second number
cont = "y"
while cont == "Y" or cont == "y":
#Exception that will check the user input
try:
lr = int(input("Enter your lower range:"))
hr = int(input("Enter your higher range:"))
num1=float(input("Enter your first number:"))
num2=float(input("Enter your second number:"))
print()
#If the values are in range, program prints results
if Mylib.inRange(lr, hr, num1, num2)== True:
print("The result of", num1, "+", num2, "=", Mylib.add(num1, num2))
print("The result of", num1, "-", num2, "=", Mylib.subtract(num1, num2))
print("The result of", num1, "*", num2, "=", Mylib.multiply(num1, num2))
print("The result of", num1, "/", num2, "=", Mylib.divide(num1, num2))
print ()
#Ask user if they want to continue or exit
cont = input("Continue Looping (Y/N) :")
if cont == "N" or cont == "n":
print("Thanks for using our calculator ")
break
except:
print("Please enter a valid number.")
#My Library (Mylib.py)
#Function to add, subtract, multiply and divide two numbers
def add(num1,num2):
add = num1 + num2
return add
def subtract(num1, num2):
sub = num1 - num2
return sub
def multiply(num1, num2):
mult = num1 * num2
return mult
def divide(num1, num2):
#Exception will check the divide by zero exception
try:
divide = num1 / num2
return divide
except ZeroDivisionError:
return "Cannot divide by zero!"
#Are numbers entered in range or not in range?
def inRange(lr, hr, num1, num2):
if lr return True else: print("The input values are outside the input ranges,") print("Please check the number and try again.") print("Thanks for using our calculator.") #This number takes a string in the format: "N1, N2, operator" #and performs the operation between N1 and N2 as indicated by #the operator. def scalc(p1): splits = p1.split(",") #Parse the first and second number num1 = float(splits[0]) num2 = float(splits[1]) #Parse the operator operator = splits[2] if operator == "+": return add(num1, num2) elif operator == "-": return subtract(num1, num2) elif operator == "*": return multiply(num1, num2) elif operator == "/": return divide(num1,num2) else: return "Invalid operator"
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
