简体   繁体   English

列表索引超出范围

[英]List index out of range

How do I fix this error, pos_coordinates contains a list of values and the updatePos_coordinates is the previously pos_coordiantes values copied into the updatePos_coordinates list. 如何解决此错误,pos_coordinates包含值列表,而updatePos_coordinates是先前复制到updatePos_coordinates列表中的pos_coordiantes值。 I want to find the differences between the object and index value 8, 9, 12, and 13. but I kept on getting this error. 我想找到对象和索引值8、9、12和13之间的差异,但我一直遇到此错误。 How do I solve this 我该如何解决

    screen_width = 0
    screen_height = 0
    updatePos_coordinates = []

    while True:
        client_socket.send("loc\n")
        data = client_socket.recv(8192)
        pos_coordinates = data.split()

        if(not(pos_coordinates[-1] == "eom" and pos_coordinates[0] == "start")):
            continue

        if (screen_width != int(pos_coordinates[2])):
            screen_width = int(pos_coordinates[2])
            screen_height = int(pos_coordinates[3])

        if (pos_coordinates != updatePos_coordinates):
            if(cmp(pos_coordinates[8:10], updatePos_coordinates[8:10]) == 0):
                both.brake()
                print "Ball is in the same location.."
            if((pos_coordinates[8] - updatePos_coordinates[8] > 5) or (pos_coordinates[8] - updatePos_coordinates[8] < -5) or (pos_coordinates[9] - updatePos_coordinates[9] > 5) or (pos_coordinates[9] - updatePos_coordinates[9] < -5)):     
                activateRobot(pos_coordinates)
            else:
                both.brake()
                print "Ball is in the same location.."

        updatePos_coordinates = pos_coordinates[:]
        time.sleep(3)
        print '...........'

and I keep on getting this: 我继续得到这个:

Traceback (most recent call last):
  File "test.py", line 155, in <module>
    if((pos_coordinates[8] - updatePos_coordinates[8] > 5) or (pos_coordinates[8] - updatePos_coordinates[8] < -5) or (pos_coordinates[9] - updatePos_coordinates[9] > 5) or (pos_coordinates[9] - updatePos_coordinates[9] < -5)):     
IndexError: list index out of range

You are trying to access updatePos_coordinates[8] when it doesn't have any elements yet, as it hasn't been changed since you defined it as updatePos_coordinates = [] . 您尝试访问updatePos_coordinates[8]时还没有任何元素,因为自从将其定义为updatePos_coordinates = []以来就没有更改。

You should fill updatePos_coordinates with default values before the loop. 您应该在循环之前用默认值填充updatePos_coordinates

For example: 例如:

updatePos_coordinates = ['' for _ in range(16)]
# replace 16 with length of pos_coordinates

Alternatively, you could just make both coordinates equal to each other the first time through the loop. 或者,您可以在循环中第一次使两个坐标彼此相等。

if len(updatePos_coordinates) < 16:
    updatePos_coordinates = pos_coordinates[:]

elif pos_coordinates != updatePos_coordinates:
    ...

I've also noticed that you are treating the coordinates like integers, when they are actually strings. 我还注意到,当坐标实际上是字符串时,您会将它们视为整数。

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

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