代碼編譯但輸出錯誤。例如,當我輸入45的值時,輸出爲1.0。我使用enthought canopy IDE,我哪裏出錯了?餘弦x的泰勒級數在運行時使用python提供邏輯錯誤
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放在循環之外。 – GAVD