2 Replies Latest reply on Mar 13, 2018 1:21 PM by aliern

    Using a while loop to find an approximate value of pi

    aliern

      I was given an assignment question requiring the use of Python 3 and a while loop to find an approximate value of pi (we aren't allowed to use the function).

      I was able to work something out but it does not approximate the correct value. This is the code that I came up with:

       

      import math

       

       

      #Calculate pi

      root = math.sqrt(2)

      prev = math.sqrt(2 + root)

      while (2 / math.sqrt(2 + prev) > 1):

          prev *= prev

      pi = prev

      print("Approximation of pi: %s" %(round(pi, 3)))

      print()

      #Input by user

      r = float(input("Enter the radius: "))

      print()

      #Calculate area of circle

      x = (pi * r**2)

      print("Area: %s" %(round(x, 3)))

       

      Any ideas?

       

      This is the question. I have already submitted but would like to see what the loop should actually look like.