Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

keep getting the same error, how do I fix?

beginner programmer here, so please bear with me.

I keep getting the same error, it says,

UnboundLocalError: local variable ‘x’ referenced before assignment.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading