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. I want to find the differences between the object and index value 8, 9, 12, and 13. but I kept on getting this error. 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 asupdatePos_coordinates = [].You should fill
updatePos_coordinateswith default values before the loop.For example:
Alternatively, you could just make both coordinates equal to each other the first time through the loop.
I’ve also noticed that you are treating the coordinates like integers, when they are actually strings.