代码可以编译,但输出错误。例如,当我输入值 45 时,我得到的输出为 1.0。我使用enthought canopy IDE,我哪里出了问题?
import math
x = int(raw_input("Enter the size of the angle : "))
y = (x*math.pi)/180
# my factorial function
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
def cos(x):
for i in range (9):
sum = 0
sum += ((-1)**(i)) * (y **(2* i))/factorial((2*i))
return sum
print cos(x)
print y # I wanted to be sure my conversion to radian is right
最佳答案
在每个循环中将总和设置回零:sum=0
应该在 for 循环之前,而不是在其中。
关于python - cosine x 的泰勒级数使用 python 在运行时给出逻辑错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35641935/