Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8020677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:45:15+00:00 2026-06-04T21:45:15+00:00

I am a beginner in python and programming and have already hit a roadblock

  • 0

I am a beginner in python and programming and have already hit a roadblock while doing excercises on HTLCS.

The problem is to use Liebniz approximation to calculate the value of pi (3.14…).

Here is my pitty attempt to solve the question:

def myPi():
    n = 0
    value = ((-1) ** n)/(2 * n + 1) 
    runningtotal = 0
    while True:
        runningtotal += value
        n += 1 
        value = ((-1) ** n)/(2 * n + 1)
    runningtotal *= 4

    return runningtotal

Of course, the Python interpreter shell never finishes my function because of while True, and I understand solutions like while n != 5000 also work, but I want Python to find the terminating point itself and return the result.

I attempted to run while statement until the value of runningtotal and that of the updated runningtotal are same at certain floating number, but failed because for some reason the last floating points of two values missed by one at each loop. (runningtotal: 3.14157, updated runningtotal: 3.14158 -> runningtotal: 3.14158, updated runningtotal: 3.14157 -> repeat).

This is my first question on this forum, so let me know if I didn’t make myself clear or violated the rules of Stackflow that I was unaware of.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:45:17+00:00Added an answer on June 4, 2026 at 9:45 pm

    Floating point numbers have inherent imprecision, and testing them for equality is a risky business. I would recommend using a small tolerance instead of testing for equality. Instead of oldTotal == newTotal, test for something like abs(oldTotal-newTotal)<0.0001 (or whatever tolerance you like).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been programming Python for a while and I have a very good
I'm a beginner in Python. My problem is pretty simple. I have a string
Disclaimer: I am a beginner in python but have Drupal programming experience. I have
Disclaimer: I am a beginner in python but have Drupal programming experience. I have
As part of the last assignment in a beginner python programing class, I have
I am learning Python, following a book (Python Programming for the Absolute Beginner, 3rd
I'm a beginner, with programming and Python. What I want to do is read
I am a beginner in Python and I have written this simple code but
I am beginner in python, and I need to use some thirdparty function which
A python/django beginner's question: I have a datetime object (drive_date), a time object (start_time)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.