Python 为什么我的代码会给出';int';对象不可下标错误?
错误:Python 为什么我的代码会给出';int';对象不可下标错误?,python,Python,错误: t= int(input()) ar=[] chk=0 x=0 y=0 while(t>0) i=int(input()) for l in range(i): ar= int(input()) for l in range(i-1): for m in range(l+1,i): x=ar[l] y=ar[m] k=x*y if
t= int(input())
ar=[]
chk=0
x=0
y=0
while(t>0)
i=int(input())
for l in range(i):
ar= int(input())
for l in range(i-1):
for m in range(l+1,i):
x=ar[l]
y=ar[m]
k=x*y
if k in ar:
continue
else:
chk=chk+1
print(True)
if chk>0:
print(False)
t-=1
在这个程序中,如果数组中的所有对都遵循关系x=a*b,则我试图打印true,其中x是数组中的任意元素,a abd b是该对的元素。您要做的是将输入附加到
ar
。使用
TypeError: 'int' object is not subscriptable
而不是
ar.append(int(input())
它将
ar
类型从list
更改为int
,并且不再能够使用索引访问它。您使用的ar
错误。首先像列表一样使用ar
,然后像int一样使用
ar = int(input())
您必须附加值:
ar=[] # You are using ar like a list
ar= int(input()) # Now, you are using ar like an int
现在你正在列一个很好的清单
ar
是一个整数:ar=int(input())
。您不能为整数下标。在上面的循环中,将ar[l]=int(input())
而不是ar=int(input())
我投票决定结束,因为这只是一个输入错误。
ar=[] # You are using ar like a list
ar= int(input()) # Now, you are using ar like an int
ar.append(int(input()))