Question

# Description: This program is a function which calculates pi #per Leibniz formula,based on the number...

# Description: This program is a function which calculates pi #per Leibniz formula,based on the number of values passed to it.

# Programmer: xxxxx

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))

How many terms: 12

3.058402765927333

HW

In Puthon

Modify the program above so that it includes exception handling, is “debugged”, and allows the user to execute the program multiple times without exiting it. Be sure to correct any issues found if there is any.

Homework Answers

Answer #1
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)? ")

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
This program will output a right triangle based on user specified height triangle_height and symbol triangle_char....
This program will output a right triangle based on user specified height triangle_height and symbol triangle_char. (1) The given program outputs a fixed-height triangle using a * character. Modify the given program to output a right triangle that instead uses the user-specified triangle_char character. (1 pt) (2) Modify the program to use a loop to output a right triangle of height triangle_height. The first line will have one user-specified character, such as % or *. Each subsequent line will have...
Part II - Pi Calculation implementation. If you watch the Discovery or Sci-Fi channel you will...
Part II - Pi Calculation implementation. If you watch the Discovery or Sci-Fi channel you will find dozens of alien conspiracy shows that reference Pi as something so advanced that it must be alien.  The number πis a mathematical constant, the ratio of a circle's circumference to its diameter, commonly approximated as 3.14159. So you could graph the value or calculate an approximate value using the series given below: Pi = 4 * (1/1 – 1/3 + 1/5 – 1/7 +...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*;...
1) Consider the following Java program. Which statement updates the appearance of a button? import java.awt.event.*; import javax.swing.*; public class Clicker extends JFrame implements ActionListener {     int count;     JButton button;     Clicker() {         super("Click Me");         button = new JButton(String.valueOf(count));         add(button);         button.addActionListener(this);         setSize(200,100);         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);         setVisible(true);     }     public void actionPerformed(ActionEvent e) {         count++;         button.setText(String.valueOf(count));     }     public static void main(String[] args) { new Clicker(); } } a. add(button);...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
I did already posted this question before, I did get the answer but i am not...
I did already posted this question before, I did get the answer but i am not satisfied with the answer i did the code as a solution not the description as my solution, so i am reposting this question again. Please send me the code as my solution not the description In this project, build a simple Unix shell. The shell is the heart of the command-line interface, and thus is central to the Unix/C programming environment. Mastering use of...
CASE STUDY – Jacobson Carpet Company In January 2002, Ms. Mary Lewis was preparing to meet...
CASE STUDY – Jacobson Carpet Company In January 2002, Ms. Mary Lewis was preparing to meet with Mr. Carpenter, President of Jacobson Carpet Company. Ms. Lewis assumed that the meeting was related to the recent Board of directors of the company. As a direct assistant to the President, she knew from experience that this type of meeting often resulted in a project to be studied. Her expectation was confirmed as soon as Mr. Carpenter began to inform her of the...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From the April 2004 Issue Save Share 8.95 In 1991, Progressive Insurance, an automobile insurer based in Mayfield Village, Ohio, had approximately $1.3 billion in sales. By 2002, that figure had grown to $9.5 billion. What fashionable strategies did Progressive employ to achieve sevenfold growth in just over a decade? Was it positioned in a high-growth industry? Hardly. Auto insurance is a mature, 100-year-old industry...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT