Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/323.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的s近似_Python - Fatal编程技术网

计算斯特林';用户输入Python的s近似

计算斯特林';用户输入Python的s近似,python,Python,我是一个巨蟒迷,我的任务是 编写一个程序,使用斯特林近似计算n阶乘。 您的程序应提示用户输入n的值,然后计算n!的斯特林近似值,并输出“近似值为XXX,即精确答案的YY”,其中XXX为斯特林近似值,YY为XXX/n!。(如果近似值良好,YY的值应接近1)。” 我写 import math n = input(int("Enter a value for n: ") XXX = math.sqrt(2*math.pi*n)*(n/math.e)**n YY = XXX/n

我是一个巨蟒迷,我的任务是

编写一个程序,使用斯特林近似计算n阶乘。 您的程序应提示用户输入n的值,然后计算n!的斯特林近似值,并输出“近似值为XXX,即精确答案的YY”,其中XXX为斯特林近似值,YY为XXX/n!。(如果近似值良好,YY的值应接近1)。”

我写

import math

n = input(int("Enter a value for n: ")

XXX = math.sqrt(2*math.pi*n)*(n/math.e)**n
YY = XXX/n          

print("The approximation is XXX which is YY of the exact answer")
这可能是完全错误的,有什么简单的方法吗?

这可能会对您有所帮助

import NumPy as np
def f(n):
    if (np.sqrt((2*np.pi)) *n**( (n+.5))*np.exp(-n) < np.math.factorial(n) \
        <np.sqrt((2*np.pi)) *n**( (n+.5))*np.exp(-n+1/(12*n))):
        print("Stirling is true")
    else:
        print("Something is wrong")

我在您的
input
语句行中看到语法错误(缺少右括号)。而且,您应该围绕输入执行
int
类型转换,而不是内部转换。另外,最后的print语句没有输出
XXX
YY
print的值(“近似值是{0},它是精确答案的{1}”。format(XXX,YY))
总的来说非常适合第一次尝试。你为什么不根据你得到的帮助给出你自己的答案呢?我要输入数学n=int(输入(“为n输入一个值”))XXX=math.sqrt(2*math.pi*n)*(n/math.e)**nyy=XXX/n print(“近似值是{0},它是精确答案的{1}”。格式(XXX,YY))
f(5)