Question

In: Computer Science

Python Assignement Programm Assignement 1 # Description: This program is a function which calculates pi per...

Python Assignement

Programm Assignement 1

# Description: This program is a function which calculates pi per Leibniz

formula,based on the number of values passed to it.

def calculate_pi(n):

    total, sign = 0, 1

    for i in range(n):

        term = 1 / (2 * i + 1)

       total += term * sign

        sign *= -1

    total *= 4

    return total

n = int(input("How many terms: "))

print(calculate_pi(n))

PRogramm Assignement2

# This program is to modify the program for Homewor

k Assignment 1

so that it includes exception

# handling, is

, and allows the user to execute the program

multiple times

# without exiting it.

def calculate_pi(n):

   try:

       total, sign = 0, 1

       for i in range(n):

           term = 1 / (2 * i + 1)

           total += term * sign

           sign *= -1

       total *= 4

       return total

   except Exception as e:

       print("Exception occurred..")

       return 0

choice = "yes"

while(choice=="yes"):

   n = int(input("How many terms: "))

   print(calculate_pi(n))

   choice = input("Want to run it again (yes/no)? "

)

PB

Divide the program for Assignment 2 into two modules: 1. A class module which contains a class based on the Leibniz formula, including a constructor for use with the class, and 2. A program module which creates an instance of the class defined in the class module and uses the class attribute to perform the functionality required in Assignement 2. Be sure to correct any issues found with Assignement 2.

Solutions

Expert Solution

#source code:

class Leibniz:
   def __init__(self):
       self.total=0
          
   def calculate_pi(self,n):
       try:
           self.total, sign = 0, 1
           for i in range(n):
               term = 1 / (2 * i + 1)
               self.total += term * sign
               sign *= -1
               self.total *= 4
           return self.total
       except Exception as e:
           print("Exception occurred..")
           return 0
class Main_Calc():
   choice = "yes"
   while(choice!="no"):
       try:
           n = int(input("How many terms: "))
           Object_pi=Leibniz()
           print(Object_pi.calculate_pi(n))
           choice = input("Want to run it again (yes/no)? ")
       except:
           print("Enter the Integer")
          
if __name__=="__main__":
       Call_class=Main_Calc()

#output:

#if you have any doubts comment below.,.


Related Solutions

Write a python program that calculates the average number of words per sentence in the input...
Write a python program that calculates the average number of words per sentence in the input text file. every sentence ends with a period (when, in reality, sentences can end with !, ", ?, etc.) and the average number of sentences per paragraph, where a paragraph is any number of sentences followed by a blank line or by the end of the text.
Need the following program in Python: Create a program that calculates the estimated duration of a...
Need the following program in Python: Create a program that calculates the estimated duration of a trip in hours and minutes. This should include an estimated date/time of departure and an estimated date/time of arrival. Arrival Time Estimator Enter date of departure (YYYY-MM-DD): 2016-11-23 Enter time of departure (HH:MM AM/PM): 10:30 AM Enter miles: 200 Enter miles per hour: 65 Estimated travel time Hours: 3 Minutes: 5 Estimated date of arrival: 2016-11-23 Estimated time of arrival: 01:35 PM Continue? (y/n):...
Python Problem 4. Estimate pi version 2: write a program that will quickly estimate pi to...
Python Problem 4. Estimate pi version 2: write a program that will quickly estimate pi to a precision of 1e-4 using a monte carlo approach. Your program should employ a loop where each iteration produces a new x,y pair. One will then determine whether the x,y pair lies inside or outside the circle. (see below) Since all the points will land within the square shown above but only a fraction will land within the circle, we can estimate pi by...
Hotel Occupancy C++ Only Program Description Write a program that calculates the occupancy rate for a...
Hotel Occupancy C++ Only Program Description Write a program that calculates the occupancy rate for a hotel. The program should read its data from a file named "hotel.dat". The first line of this file will be a single integer specifying the number of floors in the hotel. Each of the remaining (N) lines will have two integers; the number of rooms on that floor and the number of occupied rooms on that floor. Your program should display the following: How...
Write a python code that calculates π using numpy rand function.
Write a python code that calculates π using numpy rand function.
Write a Python program which uses a function to calculate the perimeter of a rectangle. a...
Write a Python program which uses a function to calculate the perimeter of a rectangle. a function named volume to calculate the volume of a cylinder volume = 3.14 x radius x radius x height .b function named volume to calculate the volume of a cuboid volume = Length x width x ht Write a Python Program to calculate the sum of all odd numbers for 2 to 20 using a for loop. 4. Write statements that assign random integers...
Write a program that contains a function that takes in three arguments and then calculates the...
Write a program that contains a function that takes in three arguments and then calculates the cost of an order. The output can be either returned to the program or as a side effect. 1. Ask the user via prompt for the products name, price, and quantity that you want to order. 2. Send these values into the function. 3. Check the input to make sure the user entered all of the values. If they did not, or they used...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the...
In Python: Write a function called sum_odd that takes two parameters, then calculates and returns the sum of the odd numbers between the two given integers. The sum should include the two given integers if they are odd. You can assume the arguments will always be positive integers, and the first smaller than or equal to the second. To get full credit on this problem, you must define at least 1 function, use at least 1 loop, and use at...
In Python write a program that calculates and prints out bills of the city water company....
In Python write a program that calculates and prints out bills of the city water company. The water rates vary, depending on whether the bill is for home use, commercial use, or industrial use. A code of r means residential use, a code of c means commercial use, and a code of i means industrial use. Any other code should be treated as an error. The water rates are computed as follows:Three types of customers and their billing rates: Code...
on python 3.6: Write a program that will quickly estimate pi to a precision of 1e-4...
on python 3.6: Write a program that will quickly estimate pi to a precision of 1e-4 using Archimedes approach of averaging polygon perimeters. Your program should employ a loop where the number of sides of the polygon are increased each iteration until the absolute value of the difference between the current estimate and the previous estimate is less than 1e-4. l. The program should output the estimate of pi and the number of iterations needed to reach the desired level...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT