0

so I have this cellular automata project located here on repl https://replit.com/@lighteningboltb/cellular-automata

in it I have two dictionaries grid and grid2. grid is not supposed to be edited during computation of the next step except this one line of code fullcompute grid = grid2 that is the only thing that should change grid but somewhere all of the values of grid get cleared and I don't know why. I use the values

height? 5
length? 5
move amount? 1
up1
right2
left3
down4

for testing and my goal is to have the last grid look like

0 0 0 0 0
0 1 1 1 0
0 1 2 1 0
0 0 1 1 0
0 0 0 0 0

but it always looks like this because grid gets cleared

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0

all I need to know is why a. grid2 gets cleared then making grid clear b. why grid gets cleared thanks

  • without looking at your code, I presume you think you clone the grid by assigning it, but in fact you don't, you just copy its reference. If it's a list, clone it like this: `grid = grid2[:]`. If it's a dictionary: `grid = grid2.copy()` – Walter Tross Jun 03 '22 at 21:43
  • See also: https://stackoverflow.com/questions/2465921/how-to-copy-a-dictionary-and-only-edit-the-copy – Walter Tross Jun 03 '22 at 21:47

0 Answers0