Question
I have been given the following code for this question in python but it does not print I do not know what the problem is
I have been given the following code for this question in python but it does not print I do not know what the problem is and I need it to have a 2D list :
def initialize_grid(): grid = [[0] * 20 for _ in range(20)] while True: coordinates = input("Enter the coordinates of a live cell (row, column) or type 'done': ") if coordinates == 'done': break row, col = map(int, coordinates.split(',')) grid[row][col] = 1 return grid
def print_grid(grid): for row in grid: for cell in row: print('X' if cell else '.', end='') print()
def get_neighbor_count(grid, row, col): count = 0 for i in range(row-1, row+2): for j in range(col-1, col+2): if i == row and j == col: continue if 0
def update_grid(grid): new_grid = [[0] * 20 for _ in range(20)] for row in range(len(grid)): for col in range(len(grid[row])): neighbor_count = get_neighbor_count(grid, row, col) if grid[row][col] == 1: if neighbor_count == 2 or neighbor_count == 3: new_grid[row][col] = 1 else: if neighbor_count == 3: new_grid[row][col] = 1 return new_grid
def main(): grid = initialize_grid() print_grid(grid) while True: input("Press enter to continue or type 'quit' to exit.") grid = update_grid(grid) print_grid(grid) if sum(sum(row) for row in grid) == 0: print("All cells are dead.") break
The Game of Life was devised by mathematician John Conway in 1970. It models a very simple world. The Life world is a two-dimensional plane of cells. Each cell may be empty or contain a single creature. Each day, creatures are born or die in each cell according to the number of neighboring creatures on the previous day. A neighbor is a cell that adjoins the cell either horizontally, vertically, or diagonally. The rules in pseudocode style are: if (the cell is alive on the previous day) \{ if (the number of neighbors was 2 or 3 ) \{ the cell remains alive \} else \{ the cell dies \} } else if (the cell is not alive on the previous day) \{ if (the number of neighbors was exactly 3 ) \{ the cell becomes alive t else \{ the cell remains dead For example, the world displayed as: 0000000000 0000000000 000XXX0000 0000000000 0000000000 0000000000 where X's indicate living cells, becomes: 000000000000000000000000000000000000000000000000000000000 Create a Life application that has a 2020 grid. To initialize the grid, the application should prompt the user for the coordinates of live cells on the first day. The application should then generate each day's world as long as the user wishes to continue or until there are no more live cellsStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started