类型错误:';int';在python中引用2d数组时,对象不可下标
我试图在一份参赛者人数和分数列表中找到最低的分数。在第6行,我得到一个错误:类型错误:';int';在python中引用2d数组时,对象不可下标,python,typeerror,Python,Typeerror,我试图在一份参赛者人数和分数列表中找到最低的分数。在第6行,我得到一个错误: if score[u[1]] == score[0]: TypeError: 'int' object is not subscriptable 我试图通过循环检查整个2d数组,以找到哪一个分数与获得的最低分数相匹配,并检索参赛者编号 for x in range(contestants): CN = x+1 score1.insert(x,[[CN],[score[x]]]) score.s
if score[u[1]] == score[0]:
TypeError: 'int' object is not subscriptable
我试图通过循环检查整个2d数组,以找到哪一个分数与获得的最低分数相匹配,并检索参赛者编号
for x in range(contestants):
CN = x+1
score1.insert(x,[[CN],[score[x]]])
score.sort
for u in score:
if score[u[1]] == score[0]:
KO = score[u[0]]
print (KO)
分数如下所示:
for i in range (contestants):
j1 = int(input("Judge 1 enter your score for the contestant: "))
j2 = int(input("Judge 2 enter your score for the contestant: "))
j3 = int(input("Judge 3 enter your score for the contestant: "))
j4 = int(input("Judge 4 enter your score for the contestant: "))
j5 = int(input("Judge 5 enter your score for the contestant: "))
print("Round over, next contstant")
scores = [j1,j2,j3,j4,j5]
scoreJ1.append(scores[0])
scoreJ2.append(scores[1])
scoreJ3.append(scores[2])
scoreJ4.append(scores[3])
scoreJ5.append(scores[4])
scores.sort()
scores.pop(0)
scores.pop(3)
#proud of this
score.insert(i,scores[1]+scores[2]+scores[0])
谢谢您的帮助。您收到的错误消息是
TypeError:“int”对象不可订阅
。让我们来分析一下:
告诉您试图在某处应用不适当的操作TypeError
告诉您,不适当的操作是将一对不可订阅
放在某物之后[]
告诉您正在尝试对int执行此操作“int”对象不可下标
现在让我们来考虑产生这个错误的代码行:
if score[u[1]] == score[0]:
请注意,有三对[]
直接放在something后面:其中一对something必须具有typeint
。因此有3名候选人
得分
u
得分
score
或u
是一个整数
查看您提供的上下文
for u in score:
如果score
不合适,此行将失败int
s是不可数的,因此我们得出结论,score
不是整数。这使我们得出结论,u
(这是容器得分的某个元素)是一个整数
关于代码的其他注释(其中包含许多问题):
在下一行中使用score
时,您在一行中引用了score1
(您在其他任何地方都不引用该代码),这是可疑的。你确定前者不应该只是得分吗
score.sort
访问score
的sort
方法,但不调用它。如果您想将该方法存储在某个地方,或者将其传递到某个地方,但您没有这样做,那么在Python中这是有意义的。因此,您几乎肯定忘记了调用该函数:您可以通过在函数之后添加()
来完成此操作,例如:store.sort()
在第二个代码块scoreJ1
中,朋友似乎没有在任何地方定义,因此此代码无法工作,除非您没有显示其他内容
这应该会给你很多思考。你应该向我们展示一下分数
是什么样子的。请在你的问题中添加代码,而不是在评论中,这样会更容易阅读。关于这一点,现在应该可以了。非常感谢你的详细帮助,回答你的问题:1score1
是另一个变量yes。2.我想知道为什么score.sort
不能正常工作哈哈,谢谢!3.这是因为我没有把整个代码放在帖子里,这只是代码的一小部分,但是谢谢你的帮助concern@ThomasAyling如果您认为答案充分解决了您的问题,那么您可能会接受和/或投票(使用箭头和左边缘的勾号)。