Create a class that contains all prior functions, you must convert the functions to methods of that
Question:
Create a class that contains all prior functions, you must convert the functions to methods of that class (do not create static methods, see the lesson for the correct way to create a class)
Test the application (You can create your own application to demonstrate the usage of the class library)
Sample output
Use the same output from the last assignment.
Submission Instructions:
Make sure that you save your code in a text file in this format.
program:
W7_firstname_lastname.py
(You must create at least one object from the class library)
and this is my code any recommendations on how to fix would be appreciated
from R_Mylib import Calculator
if __name__ == "__main__":
loop = "Y"
while loop == "Y":
try:
Calculator.menu()
print("")
choice = int(input("Enter your choice: "))
if choice in [1, 2, 3, 4, 6]:
lr = float(input("Enter your Lower range---> "))
hr = float(input("Enter your Higher range---> "))
num1 = float(input("Enter your First number---> "))
num2 = float(input("Enter your Second numner---> "))
calculator = Calculator(lr, hr, num1, num2,)
if (calculator.IsInRange(num1) and calculator.IsInRange(num2)):
if(choice == 1):
print(f"The result of {num1} + {num2} =", calculator.add())
if(choice == 2):
print(f"The result of {num1} - {num2} =", calculator.sub())
if(choice == 3):
print(f"The result of {num1} * {num2} =", calculator.mult())
if(choice == 4):
print(f"The result of {num1} / {num2} =", calculator.div())
if(choice == 6):
calculator.AllInOne()
print("")
else:
print("The input values are outside the input ranges
Please check the numbers and try again")
elif choice == 5:
p1 = input("Enter problem sting like this, N1,N2,Operator, ")
calculator = Calculator(0, 0, 0, 0, p1)
calculator.scalc(p1)
else:
raise ValueError ("Enter a number [1-6] ")
except ValueError:
print("Please enter a valid number!")
loop = input("Continue Looping Y/N: ").upper()
print("Thanks for using our calculator")
-------------------------------------------------------------------------------------------------------------------------------------------------------
Mylib.py
class Calculator:
def __init__(self, lr=0, hr=0, num1=0 , num2=0, p1=0,):
self.lr = lr
self.hr = hr
self.num1 = num1
self.num2 = num2
self.p1 = p1
def menu():
ch = ["Add two numbers",
"Subtract two numbers",
"Multiply two numbers",
"Divide two numbers",
"Scalc",
"All In One" ]
for index, value in enumerate(ch):
print(f"{index+1}. {value}")
def add(self):
return self.num1 + self.num2
def sub(self):
return self.num1 - self.num2
def mult(self):
return self.num1 * self.num2
def div(self):
return self.num1 / self.num2
def IsInRange(self, n):
if self.lr <= n <= self.hr:
return True
else:
return False
def power (self):
return self.num1 ** self.num2
def root (self):
return self.num1 ** (1/self.num2)
def scalc(self, p1):
num1, num2, operator = p1.replace(" ", "").split(",")
self.num1, self.num2, = float(num1), float(num2)
if(operator == "^"):
print(f"The Result of {self.num1} ^ {self.num2} =", self.power())
elif(operator == "~"):
print(f"The Result of {self.num1} ^ {self.num2} =", self.root())
elif(operator == "+"):
print(f"The Result of {self.num1} + {self.num2} =", self.add())
elif(operator == "-"):
print(f"The Result of {self.num1} - {self.num2} =", self.sub())
elif(operator == "*"):
print(f"The Result of {self.num1} * {self.num2} =", self.mult())
elif(operator == "/"):
print(f"The Result of {self.num1} / {self.num2} =", self.div())
def AllInOne(self):
res = { "add": self.num1 + self.num2,
"sub": self.num1 - self.num2,
"mult": self.num1 * self.num2,
"div": self.num1 / self.num2 }
print("res =", res)
print ("{} + {} = {}".format ((self.num1),(self.num2),(res["add"])))
print ("{} - {} = {}".format ((self.num1),(self.num2),(res["sub"])))
print ("{} * {} = {}".format ((self.num1),(self.num2),(res["mult"])))
print ("{} / {} = {}".format ((self.num1),(self.num2),(res["div"])))
Income Tax Fundamentals 2013
ISBN: 9781285586618
31st Edition
Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill