keep getting the same error, how do I fix?

Question:

beginner programmer here, so please bear with me.

I keep getting the same error, it says,

UnboundLocalError: local variable ‘x’ referenced before assignment.

what should I do to solve this?? the code isn’t done yet, so for debugging, the values i picked for level is 1, x = 0, y = 8, and direction south.

print("Welcome to the Maze!")

#______________ LEVEL SELECT ______________

level = int(input("Select a Level: 1,2, or 3")) #level select 
while level > 3 or level <1:
  print("Invalid input. Try again.")
  level = int(input("Select a Level: 1,2, or 3"))
  grid = level + 2
if level == 1:
  maze = [[1,"O",1],[0,0,0],[0,"_",1],
            [1,0,0],[0,0,0],[0,"_",1],
            [1,0,1],[1,0,0],[0,"X",1]]
elif level == 2:
  print("level2")
elif level == 3:
  print("level3")

#______________ END OF LEVEL SELECT ______________
location = x = int(input("Select a starting point"))
endpoint = y = int(input("Select an ending point"))

location = maze[x][1]
endpoint = maze[y][1]

#_______________FUNCTION LIST ______________
def moveEast(): #MOVE EAST
  if maze[x][2] == 1:
    print("Invalid move.")
  else:
    maze[x][1], maze[x+1][1] = maze[x+1][1], maze[x][1]
    x += 1 

def moveWest(): #MOVE WEST
  if maze[x][0] == 1:
    print("Invalid move.")
  else:
    maze[x][1], maze[x-1][1] = maze[x-1][1], maze[x][1]
    x -= 1

def moveNorth(): # MOVE NORTH
  if maze[x+grid][1] == "_":
    print("Invalid Move.")
  else:
    maze[x][1], maze[x+grid][1] = maze[x+grid][1], maze[x][1]
    x += grid

def moveSouth(): # MOVE SOUTH
  if maze[x-grid][1] == "_":
    print("Invalid Move.")
  else:
    maze[x][1], maze[x-grid][1] = maze[x-grid][1], maze[x][1]
    x -= grid

#______________END OF FUNCTION LIST______________________


#add available directions
if location != endpoint:
  move = input("Which direction will you take?")
  if move == "North":
    moveNorth()
  elif move == "South":
    moveSouth()
  elif move == "East":
    moveEast()
  elif move == "West":
    moveWest()
Asked By: secretlymaia

||

Answers:

Within the scope of any given function, a local variable is a variable that is bound (assigned to) inside the function. A nonlocal or global variable is one that is defined outside the function.

If a variable is (re)bound anywhere inside the function, it is by default considered to be local in the entire function, which means that if you attempt to access its value before a value has actually been assigned, you will get an error.

You can get around this by explicitly declaring a variable to be global before you assign to it; this indicates that when you assign to the variable, you’re actually rebinding a global variable, not creating a new local variable:

def moveEast():
    global x
    if maze[x][2] == 1:
        print("Invalid move.")
    else:
        maze[x][1], maze[x+1][1] = maze[x+1][1], maze[x][1]
        x += 1

This is generally considered to be bad practice, though; as your program grows, you’ll have a lot of different variables, and if they’re all in the global namespace, keeping track of them will be difficult.

One option might be to have each function that manipulates x take its current value as an argument and return the modified value:

def moveEast(x):
    if maze[x][2] == 1:
        print("Invalid move.")
    else:
        maze[x][1], maze[x+1][1] = maze[x+1][1], maze[x][1]
        x += 1
    return x

When you call the function, you pass it x and assign the result back to x:

  elif move == "East":
    x = moveEast(x)
Answered By: Samwise
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.