Yusuf Hendricks
Yusuf Hendricks

Reputation: 15

How to return a grid as a function?

So i have an assignment where i have to use a test code to check if my code is working, what im trying to do is define a grid as a function and return it so that the test code can use it.

The test code:

def run_test (test):
    if test == 0:
        grid = [] 
        print(grid)   
        util.create_grid (grid)
        # import from a different file
        print (len (grid))
        print (len (grid[0]))
        print (len (grid[1]))
        print (len (grid[2]))
        print (len (grid[3]))
        print (grid[0][0])
        print (grid[1][2])
        print (grid[2][1])
        print (grid[3][3])

My code(from a different file):

def create_grid(grid):
  
  grid = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
  
  return grid

The error im getting: print (len (grid[0])) IndexError: list index out of range

Upvotes: 0

Views: 828

Answers (3)

Thomas Weller
Thomas Weller

Reputation: 59289

If all files are under your control and you can modify them , I see two problems:

  1. The parameter grid in this method is never used. It should be removed

    def create_grid(grid):  
        grid = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
        return grid
    
  2. The return value is never used. It should be

     grid = util.create_grid()
    

If you can't modify test_run(), because that's out of control, you need to find a way to modify the array without an assignment.

In that case, start with

grid.clear()

to ensure the array will be empty. After that add values using

grid.append()

or

grif.extend()

Upvotes: 1

Noé Mastrorillo
Noé Mastrorillo

Reputation: 309

In your create_grid() function, grid variable is only in the scope of the function, so it will edit your argument only in your function.

Example:

>>> grid = []
>>> def create_grid(grid):
...     grid = [1, 2, 3]
...
>>> create_grid(grid)
>>> print(grid)
[]

Here you can see that your grid variable didn't change.

What you should do is to return your grid after your changed it (you already did that) and define your grid variable in your run_test() function to the return value of the function like that:

>>> grid = []
>>> def create_grid(grid):
...     grid = [1, 2, 3]
...     return grid
...
>>> grid = create_grid(grid)
>>> print(grid)
[1, 2, 3]

Here your grid variable did change.

Note that here you do not need to pass your grid as argument as you redefine it in your function anyway.

So your code should looks like that:

def create_grid():
  grid = [[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,0]]
  return grid
grid = util.create_grid()

If your want to learn more about scopes, please refer to this link.

Upvotes: 0

chepner
chepner

Reputation: 531165

The issue is that you are ignoring the return value of create_grid:

grid = util.create_grid(grid)

Given that you never use grid before reassigning it, it would be simpler to pass any object as the argument (the function ignores it anyway):

if test == 0:
    grid = util.create_grid([])

Upvotes: 1

Related Questions