2

I made a code to make the summary of the numbers of a matrix but i get this error TypeError: 'range' object is not callable and i don't know why Here is my code:

print ('The summary of the positive and negative numbers of a matrix')
A=[[0 for i in range (col)] for j in range (fil)]
fil=int(input('Number of columns'))
col=int(input('Number of rows'))
auxp=0
auxn=0

for i in range (fil):
    for j in range (col):
        A[i][j]=int(input('Numbers of the matrix'))
for i in range (fil)(col):
    for j in range (fil):
        if (A[i][j]>0):
            auxp=aup+A[i][j]
        else:
            if (A[i][j]<0):
                auxn=auxn+A[i][j]
print ('The summary of the positive numbers is ',auxp)
print ('The summary of the negative numbers is ',auxn)

TypeError                                 Traceback (most recent call last)
<ipython-input-16-83bec8a1085e> in <module>
      9     for j in range (col):
     10         A[i][j]=int(input('Numbers of the matrix'))
---> 11 for i in range (fil)(col):
     12     for j in range (fil):
     13         if (A[i][j]>0):

TypeError: 'range' object is not callable

2 Answers 2

2

I think that you should be iterating over the rows and columns of the matrix as your code does in the first for loop, i.e. like this:

for i in range(fil):
    for j in range(col):
        if A[i][j] > 0:
            ...

Your code incorrect because it is attempting to call the range object:

>>> range(fil)
range(0, 10)
>>> type(range(fil))
<class 'range'>
>>> range(fil)(col)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'range' object is not callable

So the call to range(fil) creates a new range object, and then Python attempts to call that object as though it were a function, passing col to it as an argument.

Perhaps you should read up on functions and function calls to better understand how functions work in Python.

Sign up to request clarification or add additional context in comments.

Comments

0

You probably want to change the line

for i in range (fil)(col):

to just

for i in range (fil):

The way you had it, it was a function call on the range object, and as the error message says, the result of calling range() cannot be used as a callable/function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.