I have a problem using max() inside my function. When a create a list with integers, max function works good. However when a create a list in my function and then using max() with my integer list then it gives "TypeError: 'int' object is not callable" error.
Where am I wrong and how can I fix it?
>>> 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
Because you redefine max as a int number with
max = (10**num)-1
so you can not Call a number to help you get the max value of a list. Change the variable name will be ok:
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)