max()在我的函数中给出"int"不可调用的错误

sky*_*erc 3 python typeerror

我在我的函数中使用max()时遇到问题.当使用整数创建列表时,max函数效果很好.但是当我在我的函数中创建一个列表然后使用max()和我的整数列表时,它会给出" TypeError:'int'对象不可调用 "错误.
我错在哪里,我该如何解决?

>>> a = [1,2,3,4,5] # A simple list
>>> max(a) # It works fine
>>> 5
>>> def palen(num):
...     min = 10**(num-1)
...     max = (10**num)-1
...     a=[] # Another list
...     mul=0
...     for i in range(max,min-1,-1):
...         for j in range (max,min-1,-1):
...             mul=i*j
...             if str(mul)==str(mul)[::-1]:
...                 a.append(mul)
...     return max(a)
...
>>> palen(2)
Traceback (most recent call last):
 File "<input>", line 1, in <module>
 File "<input>", line 11, in palen
TypeError: 'int' object is not callable
Run Code Online (Sandbox Code Playgroud)

lqh*_*gbl 9

因为您 max 重新定义为int数

max = (10**num)-1
Run Code Online (Sandbox Code Playgroud)

所以你不能拨打号码来帮助你获得列表的最大值.更改变量名称即可:

def palen(num):
  min_num = 10**(num-1)
  max_num = (10**num)-1
  a=[] # Another list
  mul=0
  for i in range(max_num,min_num-1,-1):
    for j in range (max_num,min_num-1,-1):
      mul=i*j
      if str(mul)==str(mul)[::-1]:
        a.append(mul)
  return max(a)

print palen(2)
Run Code Online (Sandbox Code Playgroud)


Ash*_*and 5

如果我们在程序的前面部分使用 max 作为变量名,那么我们就会遇到这个问题。建议不要使用 max/min 作为变量名称。解决方案是将早期变量重命名为 max。

x=[1,2,3]
max(x)
Run Code Online (Sandbox Code Playgroud)

输出:3

max=20
x=[1,2,3]
max(x)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述