Im writing a program and want some help with new functions. Here is the code so far: def…
- User
Im writing a program and want some help with new functions. Here is the code so far: def make_grid(rows, columns): grid = [[False] * columns for _ in range(rows)] return grid
print the grid
def draw_grid(grid): for row in grid: for cell in row: if cell: print("1", end=" ") else: print("0", end=" ") print()
flips the boolean on the cells within our painting radius
def paint_cell(grid, start_location, radius): rows = len(grid) columns = len(grid[0])
codefor i in range(max(0, start_location[0] - radius), min(rows, start_location[0] + radius + 1)): for j in range(max(0, start_location[1] - radius), min(columns, start_location[1] + radius + 1)): grid[i][j] = not grid[i][j]optimal start location based on radius
def start_position(grid,radius): row = radius column = radius return row, column
radius = 1 grid = make_grid(40,4)
Can you make a function called optimum_steps that takes radius as a parameter. It uses radius to calculate a new variable called steps and returns steps. steps = 2*radius + 1
- Assistant
Sure, here's the new function:
def optimum_steps(radius): steps = 2 * radius + 1 return steps
Now you can call this function with a radius value to get the corresponding number of steps:
radius = 3 steps = optimum_steps(radius) print(steps) # output: 7