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

标签 python typeerror

我在函数中使用 max() 时遇到问题。当创建一个带有整数的列表时,max 函数效果很好。但是,当在我的函数中创建一个列表,然后将 max() 与我的整数列表一起使用时,它会给出“ TypeError: 'int' object is not callable ”错误。
我哪里错了,我该如何解决?

>>> 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

最佳答案

因为你重新定义 max 作为 int 数字

max = (10**num)-1

所以你不能打电话来帮助你获得列表的最大值。
更改变量名就可以了:
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)

关于python - max() 在我的函数中给出 "int"不可调用错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27815199/

相关文章:

Python提取包含单词的句子

python - 类型错误:int 不可调用

python - 创建 GAE 实体时遇到问题

python - 等待 asyncio.sleep ('dict' 对象不可调用中的类型错误)

python - 类型错误:列表索引必须是整数,而不是 unicode(Telepot 检索名称)

python - 即使页面存在也获取状态代码 404

python - 存储 2D 数据的最佳方式是什么

python - gnuplot:第 1 行:无效命令

python - 如何计算经纬度多边形的面积?

python - 类型错误:Python 中 + 不支持的操作数: 'dict' 和 'str'