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 6998463
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:24:53+00:00 2026-05-27T20:24:53+00:00

#this works in python 3 def pi_sum(n): total, k = 0,1 while k <=

  • 0
#this works in python 3
def pi_sum(n):
    total, k = 0,1
    while k <= n:
        total, k = total +8 /(k *(k+2)), k + 4
    return total

#this is how i tried to fix it for python 2
def pi_sum2(n):
    total, k = 0,1
    while k <= n:
        total, k = float(total +8) /(k *(k+2)), k + 4
    return total

In python 2: for pi_sum2(1e6) I get 8.000032000112001e-12. What’s wrong here?

EDIT above my first mistake was applying float to both total and 8..
i should have done:

#this is how i tried to fix it for python 2
def pi_sum2(n):
    total, k = 0,1
    while k <= n:
        total, k = total + float(8) /(k *(k+2)), k + 4
    return total
  • 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-05-27T20:24:54+00:00Added an answer on May 27, 2026 at 8:24 pm

    You need to define your variables explicitly as floats to avoid some type coersion:

    def pi_sum(n):
        total, k = 0.0, 1.0
        while k <= n:
            total, k = total + 8.0 /(k *(k+2)), k + 4
        return total
    

    should do the trick

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

Sidebar

Related Questions

Unless I'm mistaken, creating a function in Python works like this: def my_func(param1, param2):
When you start/stop a python cherrypy service (compiled with py2exe), this works fine When
I'm using ctypes to load a DLL in Python. This works great. Now we'd
Is there a library in python that works like this? >>> resolvePath(http://www.asite.com/folder/currentpage.html, anotherpage.html) 'http://www.asite.com/folder/anotherpage.html'
Is there anyway to get tuple operations in Python to work like this: >>>
In python, I wrote this function to teach myself how **kwargs works in Python:
Somehow, this works fine in the Maya/Python script editor, but fails when it's inside
I have a very frustrating python problem. In this code fixedKeyStringInAVar = SomeKey def
I'm trying (yet again) to get better at programming, this time in python, and
I have this python code: def sqrt(x): ans = 0 if x >= 0:

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.