我对为什么抛出这个 typeError 有点迷茫。
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
evaluate_poly(polyTestTwo,-13)
File "C:\Users\robert\Desktop\programming\MIT open courseware\ps2\ps2_newton.py", line
22, in evaluate_poly
valHolder+=((poly[i]*x)**i)
TypeError: tuple indices must be integers
这是我的代码:
def evaluate_poly(poly, x):
"""
Computes the polynomial function for a given value x. Returns that value.
Example:
>>> poly = (0.0, 0.0, 5.0, 9.3, 7.0) # f(x) = 7x^4 + 9.3x^3 + 5x^2
>>> x = -13
>>> print evaluate_poly(poly, x) # f(-13) = 7(-13)^4 + 9.3(-13)^3 + 5(-13)^2
180339.9
poly: tuple of numbers, length > 0
x: number
returns: float
"""
valHolder=0.0
for i in poly:
valHolder+=((poly[i]*x)**i)
i=i+1
if i==(len(poly))-1:
return float(valHolder)
break
我为获得此特定错误而输入的输入如下。
>>>polyTest=(0.0,0.0,5.0,9.3,7.0)
>>>evaluate_poly(polyTest,-13)
知道是什么原因造成的吗?我认为元组可以将浮点数作为值?