您的位置:首页 > 移动开发 > Objective-C

[置顶] python中报错:'int' object is not callable error occur的解决方案

2017-10-03 20:01 645 查看
最近在学习python的相关知识,在写下面一段代码的时候报了一个错误:

def maxArea(height):
max = 0
for k in range(len(height)):
if k + 1 < len(height):
for m in range(k + 1, len(height)):
t = min(height[k], height[m])
c = t * abs(m - k)
max = max(max, c)
return max

print(maxArea([1, 1]))


报错如下:

TypeError: 'int' object is not callable


这里其实是自己新建的max变量与系统内嵌的max函数产生了冲突,把自己的变量修改为maxs,即可,如下:

def maxArea(height):
maxs = 0
for k in range(len(height)):
if k + 1 < len(height):
for m in range(k + 1, len(height)):
t = min(height[k], height[m])
c = t * abs(m - k)
maxs = max(maxs, c)
return maxs

print(maxArea([1, 1]))


结果是:

1


所以在命名的时候注意不能和自己使用的系统函数重名,切勿再犯这样的错误,特此记录~

参考:https://stackoverflow.com/questions/11201801/why-does-the-int-object-is-not-callable-error-occur-when-using-the-sum-funct
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: