Question
import Mylib menu = [1. Add , 2. Subtract , 3. Multiply , 4. Divide, 5. SCALC, 6. All in One] while True: try: print(*menu,
import Mylib
menu = ["1. Add" , "2. Subtract" , "3. Multiply" , "4. Divide", "5. SCALC", "6. All in One"]
while True:
try:
print(*menu, sep = " ")
sel=int(input("Please select an operation: "))
n1=int(input("Enter your lower range number "))
n2=int(input("Enter your higher range number "))
except:
print("Something went wrong, lets try again")
sel=int(input("Please select an operation: "))
n1=int(input("Enter your lower range number "))
n2=int(input("Enter your higher range number "))
else:
print("You entered", n1 , "and" , n2)
try:
n3=int(input("Enter your first number "))
n4=int(input("Enter your second number "))
except:
print("You did not enter a number, let's start over")
continue
if Mylib.IsInRange(n1, n2, n3, n4) == "true":
print("You entered", n3 )
print("You entered", n4)
addition = Mylib.scalc(str(n3) + "," + str(n4) + ",+")
subtraction = Mylib.scalc(str(n3) + "," + str(n4) + ",-")
multiply = Mylib.scalc(str(n3) + "," + str(n4) + ",*")
divide = Mylib.scalc(str(n3) + "," + str(n4) + ",/")
elif Mylib.IsInRange(n1, n2, n3, n4) == "false":
print("The input values are outside the input ranges")
print("Please check the numbers and try again")
continue
if sel==1:
print("The result for " + str(n3) + " + " + str(n4) + " = " + str(addition))
elif sel==2:
print("The result for " + str(n3) + " - " + str(n4) + " = " + str(subtraction))
elif sel==3:
print("The result for " + str(n3) + " * " + str(n4) + " = " + str(multiply))
elif sel==4:
print("The result for " + str(n3) + " / " + str(n4) + " = " + str(divide))
elif sel==5:
print("The result for " + str(n3) + " + " + str(n4) + " = " + str(addition))
print("The result for " + str(n3) + " - " + str(n4) + " = " + str(subtraction))
print("The result for " + str(n3) + " * " + str(n4) + " = " + str(multiply))
print("The result for " + str(n3) + " / " + str(n4) + " = " + str(divide))
elif sel==6:
res = Mylib.AllinOne(n3, n4)
print(str(n3) + " + " + str(n4) + " = " + str(res["add"]))
print(str(n3) + " - " + str(n4) + " = " + str(res["sub"]))
print(str(n3) + " * " + str(n4) + " = " + str(res["mult"]))
print(str(n3) + " / " + str(n4) + " = " + str(res["div"]))
else:
print("Error: Operation selection was invalid. Please Try Again")
calc = input("Restart Calculator? Enter Yes/No: ")
if (calc == "Yes"):
continue
elif (calc == "yes"):
continue
elif (calc == "No" and "no"):
print("Thank you for using my calculator")
break
else:
# thankyou message for user
print("Thank you for using my calculator")
break
Mylib
def IsInRange(n1, n2, n3, n4):
if (n1 <= n3 <= n2) and (n1 <= n4 <= n2):
return "true"
else:
return "false"
def add(n3, n4):
addition = n3 + n4
return addition
def sub(n3, n4):
subtraction = n3 - n4
return subtraction
def mul(n3, n4):
multiply = n3 * n4
return multiply
def div(n3, n4):
try:
divide = n3 / n4
except ZeroDivisionError:
divide = "Can't divide by zero, answer not given"
return divide
def scalc(p1):
mystring=p1.split(",")
if mystring[2] == "+":
answer = add(float(mystring[0]), float(mystring[1]))
if mystring[2] == "-":
answer = sub(float(mystring[0]), float(mystring[1]))
if mystring[2] == "*":
answer = mul(float(mystring[0]), float(mystring[1]))
if mystring[2] == "/":
answer = div(float(mystring[0]), float(mystring[1]))
return answer
def AllinOne(n3, n4):
add = n3 + n4
subtract = n3 - n4
multiply = n3 * n4
divide = n3 / n4
print('Res is dictionary', {"add": add, "sub": subtract, "mult": multiply, "div": divide})
return {"add": add,
"sub": subtract,
"mult": multiply,
"div": divide
}
Need to create a class to the following Mylib. All the functions need to change intomethods in the class.
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