类型错误:“范围”对象也不是没有明显的原因可调用

问题描述 投票:2回答:2

我做了一个代码,以矩阵的数量的总结,但我得到这个错误类型错误:“范围”对象不是可调用的,我不知道为什么这里是我的代码:

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
python python-3.x object range typeerror
2个回答
2
投票

我认为,你的代码不会在第一个for循环,即这样你应该是在矩阵的行和列迭代:

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

您的代码不正确,因为它正试图调用range对象:

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

所以调用range(fil)创建一个新的range对象,那么Python试图调用该对象,就好像是一个函数,传递col把它作为一个参数。

也许你应该对功能念起来函数调用,以便更好地理解函数在Python中是如何工作的。


0
投票

你可能想改线

for i in range (fil)(col):

for i in range (fil):

你有它的方式,这是range对象上的函数调用,并作为错误信息说,呼吁range()的结果不能被用作一个可调用/功能。

© www.soinside.com 2019 - 2024. All rights reserved.