debugging - Function changing the value of argument in Python -
I am trying to write a program, and I have found some very strange problems, so I step by step I am trying to debug it and try to debug it. Some strange things are going on.
If you are not familiar with Conway's life game, then the rule to determine the next step:
- At least two Any living living with living neighbors dies, as if caused by the least population.
- Any living life with two or three living neighbors lives up to the next generation.
- Any live cell with more than three living neighbors dies, such as by more and more.
- Any dead cell with only three living neighbors becomes a live cell, such as by reproduction.
I am placing a list called squareList in N_ROWS rows and N_COL columns. I refer to each element in the form of class list [i] [j] .
My get_next (SquareList) function gives another list, which 'calculates the number of neighbors' in each category, and gives another list with the next step.
Now, on my problem. Here is a test case, which indicates that the function values are changing, which is not considered to be:
squareList = init_list (NUM_ROWS, NUM_COL) Takes you to zero. #here, NUM_ROWS = 12 and NUM_COL = 18 squareists [11] [17] = 1 squarelist [5] [7] = 1 squarelist [6] [7] = 1 squarelist [7] [7] = 1 squarelist [9] [2] = 1 category list [9] [3] = 1 category list [9] [4] = 1 print_list (squarelist) # printSlashlight nextList = the_ext (squalist) #does squarelist print does not change \ n ---- - --------------------------------- \ n 'print_list (square-list) #printsvglosts again sys.exit ( )
When I use my print_list function I get what I get:
As you can see, all things touched by the get_next function are set to zero. It should not be in my mind for two reasons:
- What should be according to Conway logic in my get_next function (and I really can not find why this will not work)
- My get_next function is setting an next list variable, doing anything for squareList is not? What am I missing?
Here is the code for my get_next function:
def get_next (squareList): #gets for next list of games in the nextList = squareList category (1, 1, 1998-1) for the category (1, 1, 1998-1) in the category: Neighbors # North: Counter = Amount (for range in X ([SquareList [i + x] If not (x == y and x == 0)]) # Logic to change: if class list [i] is in the range for [j + y]) (- 1,2) y (-1,2) [J] == 1 counter & lt; 2: Next list [i] [j] = 0 elif category list [i] [j] == 1 counter & gt; 3: Next List [i] [j] = 0 Alif Class List [i] [j] == 0 and Counter == 3: Next List [i] [j] = 1 Next Return Back List
My basic idea is that this is changing a global variable, but this is not the case. First of all, the python needs to declare the global variable used within the function, and secondly, I tried to rename the list and received the same result.
EDIT: Answer to LANG Suggestions:
The first thing you can do in your get_next
function is < Code> Next list is intended for reference of the same list which indicates squareList
does not mean the assignment - both the nextList = squareList
Names indicate the same actual structure in memory, so the next Any change in the list
will affect the class
You should use to get the actual copy of your class list
list.
Comments
Post a Comment