Passing a Multidimensional Array to a Function in Python

I was just trying an maze-mouse game, to retain the position of the mouse in array,

I created a multidimensional array in python

maze = [[0 for x in range(8)] for x in range(8)]

and I called a function using

l = move_mouse(m,maze)

function is

def move_mouse(m,maze =[i][j]):
    if m=='down':
       i = i+1
       return maze

How to pass the array with with values i and j in maze so that to retain the current position and return the same to main function?

Please tell if I'm wrong in assigning it.

2

1 Answer

The maze and the position are two different objects, you cannot mix them in a way you tried. Keep them separate:

def move_mouse(m, maze, pos):
    if m == 'down':
        # check the maze
        pos = pos[0], pos[1] + 1
    return pos

maze = [[0 for x in range(8)] for x in range(8)]
pos = (4,4)

pos = move_mouse('down', maze, pos)
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

James H. Sterling

James H. Sterling

Environmental Science & Climate Journalist

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.

Share this article
Twitter Facebook Pinterest