Python程序生成TypeError:str对象不可调用



当我试图打印以下结果时,在其中一行(第26行(中出现类型错误:

def myreduce(func, lst):
x = 0
x1 = x + 1
result = lst[x]
while x1 < len(lst):
applyfunc = func(result, lst[x1]) #This is the line in question
result = applyfunc
x += 1
x1 += 1
return round(result, 1)
print(myreduce('`complex`', [3, -1, 4.5, 23, 10]))

您需要将一个函数作为第一个参数传递给myreduce函数。你正在传递字符串。

问题是在哪里调用函数。代替:

print(myreduce('`complex`', [3, -1, 4.5, 23, 10]))

你必须写:

print(myreduce(complex, [3, -1, 4.5, 23, 10]))

最新更新