Question
I need help using the unittest function in python. The homework assignment is attached. The assignment is assignment 6. Instructions for Homework Assignment 6 Write
I need help using the unittest function in python. The homework assignment is attached. The assignment is assignment 6.
Instructions for Homework Assignment 6 Write a program which imports unittest and then uses it to verify all the methods of the class which you wrote for Homework Assignment 4.
BACKGROUND INFO: Assignments 1-5 Assignment 5 Add a function to the program module you wrote for Homework Assignment 4 that 1. stores the values calculated by your Homework 4 Class methods to a file, and 2. prints those values out from the file to the shell.
Assignment 4 Add methods which do calculations based on the pi attribute to the class which you wrote for Homework Assignment 3. Modify your program module so that it demonstrates those new class methods.
Assignment 3 Divide the program you wrote for Homework Assignment 2 into two modules. The first module should define a class based on the Leibniz formula which has: 1. An attribute for storing the calculated value of pi, and 2. A constructor for instantiating an instance of the class. The second module should create an instance of the class defined in the class module and then use that instance to perform the functionality required
Assignment 2 Modify the program you wrote for Homework Assignment 1 so that it includes exception handling, is debugged, and allows the user to execute the program multiple times without exiting it.
Assignment 1 The Leibniz formula is a way of calculating the value of pi. Per Wikipedia: Write a program in Python that contains a function which calculates pi per the Leibniz formula, based on the number of values passed to it. The rest of the program should prompt the user for input, call the function, and print the value of pi based on the users input. (The correct answer is 3.058402765927333 when the user enters 12.) Be sure to include the standard header block and use comments in your program.
Code for the class and program is below.
Class class LeibenizClass: """A Class for use with Leibeniz formula""" # Attributes results = ""
# methods def __init__(self,x): """Creates a string with Leibeniz Formula""" a = 0 counter = 0 max_counter = x
for i in range(max_counter): a = a + ((-1) ** (i)) * (1 / (2 * i + 1)) self.results = str(a*4)
def Circumference(self, radius): """Calculate Circumference of Circle""" pi = float(self.results) return 2*pi*radius
def Area(self, radius): """Calculate Area of Circle""" pi = float(self.results) return pi*pow(radius, 2)
def Volume(self, radius): """Calculate Volume of Solid""" pi = float(self.results) return (4/3)*pi*pow(radius, 3)
def Surface(self, radius): """Calculate Surface of Solid""" pi = float(self.results) return 4*pi*pow(radius, 2)
Program (Assignment 1-5)
import KnoxHomework5Class
def errorMessages(): """This function contains the reusable error messages.""" print("You entered an invalid value. You must enter a positive integer.") main()
def ending(): """Allows the user to run the program again or terminate it.""" print(" " "Type y to run the program again. " "Press Enter to end the program.") runagain = input().lower() if runagain == "y": main() else: exit(0) def writeResultToFile(filename, obj, radius): file = open(filename, 'w') print("PI: ", obj.results, file=file) print("Circumference: ", obj.Circumference(radius), file=file) print("Area: ", obj.Area(radius), file=file) print("Volume: ", obj.Volume(radius), file=file) print("Surface: ", obj.Surface(radius), file=file) file.flush() file.close()
def readResultFromFile(filename): with open(filename) as f: for line in f.readlines(): print(line.strip())
def main(): """This function controls the main thread of the program.""" filename='output.txt' print("Please enter the number of values to be calculated:") response = input() try: max_counter = int(response) except ValueError: errorMessages() else: if max_counter
main()
class LeibenizClass: ITA Class for use with Leibeniz formula # Attributes results - #methods definit_(self,x): "Creates a string with Leibeniz Formula""" a = 0 counter = 0 max_counter = x for i in range (max_counter): a = a + ((-1) ** (i)) * self.results = str(a*4) (1 /(2 * i + 1)) def Circumference (self, radius): ""Calculate Circumference of Circle pi = float (self.results) return 2*pi radius def Area (self, radius): "Calculate Area of Circle"" pi = float (self.results) return pipow (radius, 2) def Volume (self, radius): """Calculate Volume of Solid" pi = float (self.results) return (4/3) *pi pow (radius, 3) def Surface (self, radius): """Calculate Surface of Solid""" pi = float (self.results) return 4*pi pow (radius, 2) import KnoxHomeworks class def errorMessages () : "This function contains the reusable error messages." print ("You entered an invalid value. You must enter a positive integer.") main() def ending(): "Allows the user to run the program again or terminate it." print(" " "Type y to run the program again. " "Press Enter to end the program.") runagain = input().lower() if runagain == "y": main() else: exit (0) def writeResultToFile (filename, obj, radius): file = open(filename, 'w') print ("PI: ", obj.results, file=file) print ("Circumference: ", obj.Circumference (radius), file=file) print ("Area: ", obj.Area (radius), file=file) print ("Volume: ", obj. Volume (radius), file=file) print ("Surface: ", obj.Surface (radius), file=file) file. flush() file.close() def readResult FromFile(filename) : with open (filename) as f: for line in f.readlines (): print (line.strip()) def main(): "" "This function controls the main thread of the program." filename='output.txt' print("Please enter the number of values to be calculated:") response = input() def readResult FromFile(filename) : with open (filename) as f: for line in f.readlines () : print(line.strip ) def main(): "This function controls the main thread of the program." filename='output.txt' print("Please enter the number of values to be calculated:") response = input () try: max_counter = int(response) except ValueError: errorMessages () else: if max counter
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