我希望你能帮忙。 我正在寻找一种编写后插入一个项目的函数的方法。 让我举个例子:
def general_poly(L):
"""
L, a list of numbers (n0, n1, n2, ... nk)
Returns a function, which when applied to a value x, returns the value
n0 * x^k + n1 * x^(k-1) + ... nk * x^0
"""
x = 1
res = 0
n = len(L)-1
for e in range(len(L)):
res += L[e]*x**n
n -= 1
return res
我原以为只需在此处给 x
赋值,一旦执行 general_poly(L)(10)
,它就会被替换为 x = 10
,但显然这并不容易。我需要改变/添加什么才能使函数正常运行?函数如何知道乘法是 x
?感谢你们的帮助!
x
是全局变量,你需要通过global x
显式地告诉Python。 - Christian Dean