Question
def add(num1, num2): return num1 + num2 def sub(num1, num2): return num1 - num2 def mult(num1, num2): return num1 * num2 def
def add(num1, num2):
return num1 + num2
def sub(num1, num2):
return num1 - num2
def mult(num1, num2):
return num1 * num2
def div(num1, num2):
if num2 == 0:
raise ZeroDivisionError("Cannot divide by zero")
else:
return num1 / num2
def isInRange(lr, hr, n):
if n >= lr and n <= hr:
return True
else:
return False
def promptContinue():
while True:
response = input("Would you like to perform another calculation? (y/n): ")
if response.lower() == "y":
return True
elif response.lower() == "n":
return False
else:
print("Invalid response, please enter 'y' or 'n'.")
while True:
try:
lowRange = float(input("Enter the lower range: "))
highRange = float(input("Enter the higher range: "))
num1 = float(input("Enter your First number: "))
if not isInRange(lowRange, highRange, num1):
raise ValueError("Number is outside range")
num2 = float(input("Enter your Second number: "))
if not isInRange(lowRange, highRange, num2):
raise ValueError("Number is outside range")
op = input("Enter the math operation you want to perform (+, -, *, /): ")
if op not in ["+", "-", "*", "/"]:
raise ValueError("Invalid operation selected.")
if op == "+":
result = add(num1, num2)
print("The result of", num1, "+", num2, "=", result)
elif op == "-":
result = sub(num1, num2)
print("The result of", num1, "-", num2, "=", result)
elif op == "*":
result = mult(num1, num2)
print("The result of", num1, "*", num2, "=", result)
elif op == "/":
result = div(num1, num2)
print("The result of", num1, "/", num2, "=", result)
except ValueError as e:
print("Invalid input:", e)
except ZeroDivisionError as e:
print("Error:", e)
if not promptContinue():
print("Thanks for using our calculator!")
break
How to the following with the code above?
1) Move all the functions into W5_firstname_lastname_Mylib.py
2) Use import to include W5_firstname_lastname_Mylib into the code
3) Test the code and make sure that the prior code is still working (Create your own application/test in the program using the library)
4) Add the following function into Mylib
scalc(p1)
p1 will be a string like this "N1, N2, operator"
examples
scalc("20,30,*")
the result will be 600
scalc("50,20,+")
the result will be 70
scalc("50,20,-")
the result will be 30
scalc("60,20,/")
the result will be 30
use string functions to parse the first number, the second number, and the operator from the input string.
use the prior functions (add, subtract, divide and multiply) to do the calculations.
Do not use any UI functions in the library such as print(), input(), and format. A function should accept a parameter and return at least one value to the caller.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Steps involved in moving the functions incorporating ...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