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