能帮我看看哪里有错吗

1. 计算底数e的值。

e有时被称为自然常数,是一个约等于2.71828182845904523536……的无理数,可用如下公式求自然对数的底数e的近似值:

          e=1+1/1!+1/2!+…1/n!

试设计循环程序,判断直到最后一项绝对值小于10的-6次方停止循环,注意因为计算误差原因,不同方法可能稍有区别
import math
t=1
n=1
sum=0

while t>=10**-6:
for i in range(n):
t=1/math.factorial(n)
sum+=t
print(sum)

帮我改改,谢谢

import math
t=1
n=1
sum=0
while t>=10**-6:
    t=1/math.factorial(n)
    sum+=t
    n+=1
print(sum)

img


import math
t = 1
n = 1
sum = 1
while t >= 10**-6:
    t = 1/math.factorial(n)
    sum += t
    n += 1
print(sum)

修改如下:

import math

t = 1
sum = 0

i = 0
while t >= 10 ** -6:
    i += 1
    print(f"math.factorial({i})", math.factorial(i))
    t = 1 / math.factorial(i)
    sum += t
print(sum)

输出为:

math.factorial(1) 1
math.factorial(2) 2
math.factorial(3) 6
math.factorial(4) 24
math.factorial(5) 120
math.factorial(6) 720
math.factorial(7) 5040
math.factorial(8) 40320
math.factorial(9) 362880
math.factorial(10) 3628800
1.7182818011463847


如有问题及时沟通

sum的初始值要等于1,程序如下:

import math
t=1
n=1
sum=1
while t>=10**-6:
t=1/math.factorial(n)
sum+=t
n=n+1

print(sum)
运行结果:

img


import sys

import matplotlib.pyplotasplt

sys.setrecursionlimit(1001)

deffactorial(n):

if n ==1:

return 1

else:

return factorial(n -1) * n

x = []

y = []

e =1

for i in range(1,1000):

x.append(i)

e = e +1/ factorial(i)

y.append(e)

print(e)

plt.plot(x, y)

plt.show()