Find the required program and output below: Please find the comments against eac
ID: 3798181 • Letter: F
Question
Find the required program and output below: Please find the comments against each line for the description: import math # This will import math module def calculate_pi(): # function that return value of m(i) for input i sum = 0 for j in range(0, 12): #iterate loop till 12 times sum = sum + (math.pow(-1, J)/((2 * j) + 1)) #summation of the Leibniz series pi = 4 * sum return float(pi) #return number #print the output print ("pi = ", calculate_pi()) Add these to above program: description of the program in a header block programmer name in a header block documentation string included in the function Modify the program above program so that it contains a class related to the Leibniz formula. Add additional methods to the class for doing calculations based on the pi attribute. Divide your program into two modules - one which contains your class and another which demonstrates the class methods. Enable a user to enter the number of values to be used in the pi calculation. Be sure your program contains exception handling and has been debugged. It should also launch and be usable under Windows 10 as described in class.Explanation / Answer
PROGRAM CODE:
# Class for Pi
import math #importing math for pow operation
class Pi:
sum = 0
#constructor
def __init__(self, pi_count):
self.count = pi_count
#function for calcualting pi
def calculate(self):
for j in range(0,self.count):
Pi.sum = Pi.sum+(math.pow(-1,j)/((2*j)+1))
Pi.sum = 4*Pi.sum
#Main module
#handling exceptions
try:
#getting value from user
iterations = int(input('Enter the number of values: '))
#checking if the iterations value is greater than or equal to 0
#if true - program exits
if(iterations <= 0):
print(" Invalid input")
quit()
#initializing object
pi = Pi(iterations)
#calculating pi
pi.calculate();
#printing on screen
print(" pi=")
print(Pi.sum)
#valueError - Incase of decimal or string inputs or no inputs
except ValueError:
print(" Oops! That was no valid input.")
#EOFError - when input is not provided (incase of online compilers)
except EOFError:
print(" Oops! That was no valid input.")
OUTPUT:
RUN #1:
RUN #2:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.