【发布时间】:2019-11-04 17:16:04
【问题描述】:
我正在解决麻省理工学院编程入门的问题集。
def compute_deriv(poly):
"""
Computes and returns the derivative of a polynomial function. If the
derivative is 0, returns (0.0,).
"""
der = ()
for ele in poly :
if poly.index(ele) == 0 :
continue
else:
der += tuple(ele*float(poly.index(ele)))
return der
poly = (-13.39, 0.0, 17.5, 3.0, 1.0) # x^4 + 3x^3 + 17.5x^2 - 13.39
print compute_deriv(poly)
为什么程序要遍历这一行?并返回不可迭代的错误?不应该这样
der += tuple(ele*float(poly.index(ele)))
【问题讨论】:
-
您以浮点数作为参数调用
tuple(),但您不能这样做。
标签: python loops conditional-statements