max() 在我的函数中给出 "int" not callable 错误
max() give "int" not callable error in my function
我在函数中使用 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
在读数的第 6 行,您定义了一个名为 max
的变量。这会隐藏指向 max()
的指针。
这就是您不能再使用 max()
函数的原因。
您需要在第 6 行和其他任何地方重命名您的变量 max
来解决此问题。
有关 Python 中命名空间的更多信息,请参阅我刚刚找到的这个有趣的页面:http://bytebaker.com/2008/07/30/python-namespaces/
因为你重新定义 max 为一个整数
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)
如果我们在程序的前面部分使用max作为变量名,就会遇到这个问题。建议不要使用 max/min 作为变量名。解决方案是用名称 max.
重命名较早的变量
x=[1,2,3]
max(x)
输出:3
max=20
x=[1,2,3]
max(x)
我在函数中使用 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
在读数的第 6 行,您定义了一个名为 max
的变量。这会隐藏指向 max()
的指针。
这就是您不能再使用 max()
函数的原因。
您需要在第 6 行和其他任何地方重命名您的变量 max
来解决此问题。
有关 Python 中命名空间的更多信息,请参阅我刚刚找到的这个有趣的页面:http://bytebaker.com/2008/07/30/python-namespaces/
因为你重新定义 max 为一个整数
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)
如果我们在程序的前面部分使用max作为变量名,就会遇到这个问题。建议不要使用 max/min 作为变量名。解决方案是用名称 max.
重命名较早的变量x=[1,2,3]
max(x)
输出:3
max=20
x=[1,2,3]
max(x)