Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

arrays - Python 'list indices must be integers, not tuple" error

I'm working on moving a robot around a 2d grid room of 8 x 8, and one part is initialising the sensors which consist of the closest 5 tiles around the robot.

self.sensors = [0 for x in xrange(5)]

here I'm creating an empty of array of 5 elements.

but when I attempt to set the value of sensors like this:

    if self.heading == 'East':
        self.sensors[0] = self.room[self.x, self.y-1]
        self.sensors[1] = self.room[self.x+1, self.y-1]
        self.sensors[2] = self.room[self.x+1, self.y]
        self.sensors[3] = self.room[self.x+1, self.y+1]
        self.sensors[4] = self.room[self.x, self.y+1]

I get the error of 'list indices must be integers, not tuples'.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You say self.room is a "2d grid" -- I assume it is a list of lists. In this case, you should access its elements as

self.room[self.x][self.y-1]

instead of indexing the outer list with the pair self.x, self.y-1.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...