我做了一个代码,以矩阵的数量的总结,但我得到这个错误类型错误:“范围”对象不是可调用的,我不知道为什么这里是我的代码:
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
我认为,你的代码不会在第一个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中是如何工作的。
你可能想改线
for i in range (fil)(col):
只
for i in range (fil):
你有它的方式,这是range
对象上的函数调用,并作为错误信息说,呼吁range()
的结果不能被用作一个可调用/功能。