Reputation: 1438
I am trying to trace a path made by pacman through a maze. The maze is in a list of lists. I have a (essentially) linked list of the path taken by pacman through the maze. As I"m looping through the list I would like to change every value that is on the linked list to a '*'
. I kept getting the error TypeError: 'str' object does not support item assignment
which after a bit of research, I found out that the array is immutable and I would have to rewrite the array each time or append to it. Is there a simple way for me to do this in python?
Here's the code that I have so far:
while(pacman_r != originalR or pacman_c != originalC):
i = i + 1
path.append([pacman_r, pacman_c])
grid[pacman_r][pacman_c] = '*'
pacman_r = cellGrid[pacman_r][pacman_c].parentx
pacman_c = cellGrid[pacman_r][pacman_c].parenty
I'm having a problem with the 4th line.
Upvotes: 0
Views: 76
Reputation: 907
The reason this does not work is because you are trying to edit a str
object in-place (which are immutable in python). Instead, you can re-assigning the whole thing to something meaningful, like so:
grid[pacman_r] = grid[pacman_r][:pacman_c] + '*' + grid[pacman_r][pacman_c+1:]
I did not test this, but assigning the list to a another list should work.
Upvotes: 1