简体   繁体   English

TypeError:'int'对象在python中引用二维数组时不可下标

[英]TypeError: 'int' object is not subscriptable while refrencing 2d array in python

I am trying to find the lowest score in a list of contestant numbers and scores.我试图在参赛者人数和分数列表中找到最低分。 On line 6 I get the error:在第 6 行,我收到错误消息:

if score[u[1]] == score[0]:
TypeError: 'int' object is not subscriptable

I am trying to check through the whole 2d array with the loop to find which score matches with the lowest score awarded and retrieve the contestant number我正在尝试使用循环检查整个二维数组,以找到哪个分数与授予的最低分数相匹配并检索参赛者编号

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)

Scores looks like this:分数看起来像这样:

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

Thanks for the help.谢谢您的帮助。

The error message you are getting is TypeError: 'int' object is not subscriptable .您收到的错误消息是TypeError: 'int' object is not subscriptable Let's break this down:让我们分解一下:

  • TypeError is telling you that you are trying to apply an inappropriate operation somewhere TypeError告诉您您正试图在某处应用不适当的操作
  • is not subscriptable is telling you that the inappropriate operation is putting a pair of [ ] after something. is not subscriptable告诉您不适当的操作是在某事之后放置一对[ ]
  • 'int' object is not subscriptable is telling you that you are trying to do it to an int. 'int' object is not subscriptable告诉您您正在尝试对 int 执行此操作。

Now let's consider the line of code which generated this error:现在让我们考虑产生此错误的代码行:

if score[u[1]] == score[0]:

Notice that there are three pairs of [ ] placed directly after something: one of those somethings must have type int .请注意,在某物之后直接放置了三对[ ] :其中之一必须具有int类型。 There are therefore 3 candidates因此有3名候选人

  1. score
  2. u
  3. score

So, we conclude that either score or u is an integer.因此,我们得出结论,要么score要么u是整数。

Looking at the context you provided查看您提供的上下文

for u in score:

This line would have failed if score were not some iterable.如果score不是一些可迭代的,这条线就会失败。 int s are not iterable, therefore we conclude that score is not an integer. int不可迭代,因此我们得出结论score不是整数。 Which leads us to conclude that u (which is some element of the container score ) is an integer.这使我们得出结论u (它是容器score某个元素)是一个整数。

Other comments about your code (which contains many problems):关于您的代码的其他评论(其中包含许多问题):

  1. It's suspicious that on one line you refer to score1 (to which you do not refer anywhere else) while you use score on the next. score1怀疑的是,您在一行中引用了score1 (您在其他任何地方都没有引用它),而在下一行中使用了score Are you sure that the former shouldn't just be score ?你确定前者不应该只是score吗?

  2. score.sort accesses the sort method of score but does not call it. score.sort访问scoresort方法但不调用它。 This makes sense in Python if you want to store that method somewhere, or pass it somewhere, but you are not doing that.如果您想将该方法存储在某处或将其传递到某处,这在 Python 中是有意义的,但您并没有这样做。 So you have almost certainly forgotten to call the function: you do this by adding () after it, like so: store.sort() .所以你几乎肯定忘记了调用这个函数:你可以通过在它后面添加()做到这一点,就像这样: store.sort()

  3. In your second code block scoreJ1 and friends appear not to be defined anywhere, so this code cannot work, unless there is something else that you are not showing.在你的第二个代码块scoreJ1和朋友似乎没有在任何地方定义,所以这个代码不能工作,除非你没有显示其他东西。

That should give you plenty to think about.这应该给你很多思考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM