0

I am building a maze solver using the breadth first search algorithm and I keep hitting an error that says UnboundLocalError: local variable 'starting_point' referenced before assignment. However, starting_point is a global variable, and it has already been assigned to something. When I try to use it in my program, it does not work. It worked in the function before it. That function was where I declared the variable to be global. Why does the global variable not work in the function next to it?

import queue
import re


def maze_3():
    maze3 = [
        '00000S000000000000000000000000000000000000000000000000000000000',
        '00000  000000000                                               ',
        '00000  000000000  000000000000000000  000000000000000  00000000',
        '000000            00000000    000000                      00000',
        '   00000000  000000000000         000000000000000000     000000',
        '00000000         000000000000000000000000000000   0000   000000',
        '000000        000      000000000      000     000000       0000',
        '000000  000     000000000        00000000    000000          00',
        '00  000    0  0000000000  000  000   0000  00  00 00000     000',
        '000000    000000         000000000     000  0000          00000',
        '0000000000000000            0000000  0000000   000  00000000000',
        '000000        000  0000000    0000   00000000000000    00000000',
        '0000000000000000E                                       0000000',
    ]
    return maze3


def finished_maze(maze, solution=''):
    global visited
    visited = []
    global starting_point
    starting_point= ''
    for position in range(len(maze)):
        if position == 'S':
            starting_point = maze[i]
            break
 

    j = starting_point
    k = 0
    position = set()

    for move in solution:
        if move == 'Up':
            k -= 1
        elif move == 'Down':
            k += 1
        elif move == 'Left':
            j -= 1
        elif move == 'Right':
            j += 1


    for row in range(len(maze)):
        for column in range(len(maze)):
            if (maze[row], maze[column]) in position:
                print('1 ', end='')
            else:
                print(column + '', end='')
        print()


def is_valid(maze, moves):
    a = True
    for position in range(len(maze)):
        if position == 'S':
            starting_point = maze[position]

    j = starting_point
    k = 0

    for move in re.findall('[A-Z][a-z]*', moves):
        if move == 'Up':
            k -= 1
        elif move == 'Down':
            k += 1
        elif move == 'Left':
            j -= 1
        elif move == 'Right':
            j += 1

        if not ((j >= 0 and j < len(maze[0])) and (k >= 0 and k < len(maze[0]))):
            return not a
        elif maze[k][j] == '0':
            return not a
    return a


def find_solution(maze, moves):
    a = True
    for position in range(len(maze)):
        if position == 'S':
            starting_point = maze[position]

    j = starting_point
    k = 0
    if moves in visited:
        return not a
    for move in re.findall('[A-Z][a-z]*', moves):
        if move == 'Up':
            k -= 1
        elif move == 'Down':
            k += 1
        elif move == 'Left':
            j -= 1
        elif move == 'Right':
            j += 1

        if maze[k][j] == 'E':
            print(moves)
            print(finished_maze(maze, moves))
            return a
    visited += move
    return not a


space = queue.Queue()
space.put('')
put = ''
maze = maze_3()

while not find_solution(maze, put):
    put = space.get()
    for i in ['Up', 'Down', 'Left', 'Right']:
        location = put + i
        if is_valid(maze, location):
            space.put(location)

James
  • 83
  • 1
  • 6

1 Answers1

0

global tells a function to skip creating a local function slot for the variable and instead look in the global namespace. It doesn't actually create the variable. Since you don't call finished_maze, visited is never assigned.

tdelaney
  • 73,364
  • 6
  • 83
  • 116