なぜこの 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)
何が原因ですか?タプルはフロートを値として持つことができると思いましたか?