Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我如何总结这个while循环中的不同值?_Python_Python 2.7_While Loop - Fatal编程技术网

Python 我如何总结这个while循环中的不同值?

Python 我如何总结这个while循环中的不同值?,python,python-2.7,while-loop,Python,Python 2.7,While Loop,这看起来很简单,但我很难让它工作。我试图在“while循环”中创建一个新变量来收集每个循环中x的值,如 k2 += x 但它不起作用。那么,我如何总结这个while循环中的不同值呢?多谢各位 # pi approximation by using Ramanujan Formula import math def estimate_pi(k): x = (2 * math.sqrt(2)/9801 * math.factorial(4*k) *(1103 + 26390*k))/

这看起来很简单,但我很难让它工作。我试图在“while循环”中创建一个新变量来收集每个循环中x的值,如

k2 += x
但它不起作用。那么,我如何总结这个while循环中的不同值呢?多谢各位

# pi approximation by using Ramanujan Formula

import math

def estimate_pi(k):

    x = (2 * math.sqrt(2)/9801 * math.factorial(4*k) *(1103 + 26390*k))/(math.factorial(k**4)*396**(4*k))
    while x >= 1e-15:
        k += 1
        print '{:>5.15f} {:>5} {:>1}'.format(x, 'for k =', k)
        return estimate_pi(k)

estimate_pi(0)
像这样的

def estimate_pi(k, k2=0):
    ...
    while x >= 1e-15:
        k2 += x
        ...
        return estimate_pi(k, k2)
像这样的

def estimate_pi(k, k2=0):
    ...
    while x >= 1e-15:
        k2 += x
        ...
        return estimate_pi(k, k2)

既然您提到了阶乘,我建议您看看以下内容:

通常,函数要么有while循环,要么调用自身(递归),但不能同时调用两者

while循环只是一个if语句,由于
return
语句,它不会重新进入循环。您可能正在寻找类似以下内容:

def estimate_pi(k):
    x = ...
    if x >= ...:
        print ...
        return x + estimate_pi(k+1)
    return 0

既然您提到了阶乘,我建议您看看以下内容:

通常,函数要么有while循环,要么调用自身(递归),但不能同时调用两者

while循环只是一个if语句,由于
return
语句,它不会重新进入循环。您可能正在寻找类似以下内容:

def estimate_pi(k):
    x = ...
    if x >= ...:
        print ...
        return x + estimate_pi(k+1)
    return 0

或者,你可以让k2全球化,但这可能只是一个坏主意,因为其他原因,它将工作,但

global k2
def estimate_pi(k):
  global k2
  while x >= 1e-15:
    k2+=x
    ...

或者,你可以让k2全球化,但这可能只是一个坏主意,因为其他原因,它将工作,但

global k2
def estimate_pi(k):
  global k2
  while x >= 1e-15:
    k2+=x
    ...

谢谢但是为什么这个函数需要k2的输入,而像这个阶乘函数这样的东西却不需要:def factorial(n):while n!=0:recurse=factorial(n-1)result=n*recurse返回结果else:return1@Quester要在递归调用期间记住
k2
的上一个值,我们需要传递它的值。正确,但对于我发布的阶乘函数(它使用while循环),不是这样吗使用相同的参数,我们需要在每次执行递归调用时记住变量“result=n*recurse”的值。谢谢。但是为什么这个函数需要k2的输入,而像这个阶乘函数这样的东西却不需要:def factorial(n):while n!=0:recurse=factorial(n-1)result=n*recurse返回结果else:return1@Quester要在递归调用期间记住
k2
的上一个值,我们需要传递它的值。正确,但对于我发布的阶乘函数(它使用while循环),不是这样吗使用相同的参数,每次执行递归调用时,我们都需要记住变量“result=n*recurse”的值。您说过函数不会再次进入while循环,因为有
return
语句。为什么它会在Ashwini的代码中出现?Ashwini的代码也不会重新进入while循环。请注意,while循环条件检查值“x”。但是“x”在循环中永远不会改变。因此,如果您的while循环将重新进入,那么您的代码将卡在无限循环中。您说过函数将不会再次进入while循环,因为
return
语句。为什么它会在Ashwini的代码中出现?Ashwini的代码也不会重新进入while循环。请注意,while循环条件检查值“x”。但是“x”在循环中永远不会改变。因此,如果您的while循环重新进入,您的代码将卡在一个无限循环中。