Python 赋值前引用的局部变量

Python 赋值前引用的局部变量,python,Python,大家好,我是数据结构新手,我一直在尝试用左手规则算法解决一个迷宫。然而,我一直面临着打印出解决迷宫的步骤数的困难,因为我一直面临着这个错误: UnboundLocalError: local variable 'steps' referenced before assignment 我试图初始化一个step变量,但没有成功 steps = 0 import turtle # import turtle library import sys # import sys

大家好,我是数据结构新手,我一直在尝试用左手规则算法解决一个迷宫。然而,我一直面临着打印出解决迷宫的步骤数的困难,因为我一直面临着这个错误:

UnboundLocalError: local variable 'steps' referenced before assignment
我试图初始化一个step变量,但没有成功

steps = 0


import turtle  # import turtle library
import sys    # import sys             


wn = turtle.Screen()
wn.bgcolor("grey")   # set the background colour
wn.setup(700,700)   #set the screen res

class Maze(turtle.Turtle): #new class maze
    def __init__(self):
        turtle.Turtle.__init__(self)
        self.shape("square")  # square shape , black color          
        self.color("Black")            
        self.penup()                    
        self.speed(0)                   

class End(turtle.Turtle): # new class end
    def __init__(self):
        turtle.Turtle.__init__(self)
        self.shape("square")  # square shape, green color
        self.color("green")
        self.penup()
        self.speed(0)

class White(turtle.Turtle): # new class white
    def __init__(self):
        turtle.Turtle.__init__(self)  #square shape, white color
        self.shape("square")
        self.color("White")
        self.penup()
        self.speed(0)

class sprite(turtle.Turtle): # new pointer class
    def __init__(self):
        turtle.Turtle.__init__(self)
        self.shape("arrow")
        self.color("red")
        self.setheading(270)  # starting position :point pointer to point down
        self.penup()
        self.speed(0)
        
    # following functions is used for the pointer to navigate through the maze
    
    # summary : if pointer in finish then end program, if not check if left can navigate through then navigate,
    # if left cant then go right, f right cant then turn left 90 degrees then move forward as the pointer is arldy 
    # pointing downwards 
    def spriteDown(self):
        if (self.heading() == 270):                   # checking if pointer is pointing down
            x_walls = round(sprite.xcor(),0)          # pointer x coordinates
            y_walls = round(sprite.ycor(),0)          # pointer y coordinates
            if (x_walls, y_walls) in finish:          # checking if pointer have finished the maze
                print("Finished")
                endProgram()
            if (x_walls +24, y_walls) in walls:          # checking  if they are walls on the left
                if(x_walls, y_walls -24) not in walls:   # checking if path ahead is clear
                    self.forward(24)
                    steps = steps + 1
                else:
                    self.right(90)
            else:
                self.left(90)
                self.forward(24)

   # 
    def spriteleft(self):
        if (self.heading() == 0):  # checking if pointer is pointing left 
            x_walls = round(sprite.xcor(),0)
            y_walls = round(sprite.ycor(),0)
            if (x_walls, y_walls) in finish:   # checking if pointer have finished the maze
                print("Finished")
                endProgram()
            if (x_walls, y_walls +24) in walls:       # check to see if they are walls on the left
                if(x_walls +24, y_walls) not in walls: # checking if path ahead is clear
                    self.forward(24)
                else:
                    self.right(90)
            else:
                self.left(90)
                self.forward(24)


    def spriteUp(self):
        if (self.heading() == 90):  # checking if pointer is pointing up
            x_walls = round(sprite.xcor(),0)
            y_walls = round(sprite.ycor(),0)
            if (x_walls, y_walls) in finish:   # checking if pointer have finished the maze
                print("Finished")
                endProgram()
            if (x_walls -24, y_walls ) in walls:  # check to see if they are walls on the left
                if (x_walls, y_walls + 24) not in walls: #checkiing if path ahead is clear 
                    self.forward(24)
                else:
                    self.right(90)
            else:
                self.left(90)
                self.forward(24)

    def spriteRight(self):  
        if (self.heading() == 180): # checking if pointer is pointing right 

            x_walls = round(sprite.xcor(),0)
            y_walls = round(sprite.ycor(),0)
            if (x_walls, y_walls) in finish:   # checking if pointer have finished the maze 
                print("Finished")
                endProgram()
            if (x_walls, y_walls -24) in walls:  # check to see if they are walls on the left
                if (x_walls - 24, y_walls) not in walls: #checking if path ahead is clear
                    self.forward(24)
                else:
                    self.right(90)
            else:
                self.left(90)
                self.forward(24)


def endProgram(): # end programm function
    sys.exit()

#grid for the maze
grid = [
"XXXXXXXXXXXX",
"X...X..X..eX",
"X.X....X.XXX",
"X..X.X.X.X.X",
"XX.XXX.X...X",
"X........X.X",
"XsXX...X...X",
"XXXXXXXXXXXX"
]


# setup maze
def setupMaze(grid):
    for y in range(len(grid)):                       # select each line in the grid
        for x in range(len(grid[y])):                # identify each character in the line
            character = grid[y][x]                   # assign the grid reference to the variable character
            screen_x = -288 + (x * 24)               # assign screen_x to screen starting position for x ie -588
            screen_y = 288 - (y * 24)                # assign screen_y to screen starting position for y ie  288

            if character == "X":                     # if grid character contains an X
                maze.goto(screen_x, screen_y)        # move turtle to the x and y location and
                maze.stamp()                         # stamp a copy of the turtle (white square) on the screen
                walls.append((screen_x, screen_y))   # add coordinate to walls list

            if character == "e":                     # if grid character contains an e
                end.goto(screen_x, screen_y)         # move turtle to the x and y location and
                end.stamp()                          # stamp a copy of the turtle (green square) on the screen
                finish.append((screen_x, screen_y))  # add coordinate to finish list

            if character == "s":                     # if the grid character contains an s
                sprite.goto(screen_x, screen_y)      # move turtle to the x and y location
                
                
            if character == ".":                    # if the grid character contains .
                White.goto(screen_x,screen_y)       # stamp a copy of the turtle white on the screen
                White.stamp()
            
#                 White.append((screen_x,screen_y))
                


# ############ main program starts here  ######################

maze = Maze()                # enable the maze class
sprite = sprite()            # enable the sprite  class
end = End()                  # enable End position class
White = White()             # enablewhite squares 

walls =[]                    # create walls coordinate list
finish = []                  # enable the finish array
steps = 0

setupMaze(grid)              # call the setup maze function

#check which direction is the sprite pointing to then move accoringly :
# summary: so long the program aint finish yet then continue looping, checking if the pointer is right/left/up/down then check the walls 
# then move left accrodingly till the maze is guaranteed to be solved 
while True:
        sprite.spriteRight()
        sprite.spriteDown()
        sprite.spriteleft()
        sprite.spriteUp()
        
代码中的错误发生在这几行中

def spriteDown(self):
    if (self.heading() == 270):                   # checking if pointer is pointing down
        x_walls = round(sprite.xcor(),0)          # pointer x coordinates
        y_walls = round(sprite.ycor(),0)          # pointer y coordinates
        if (x_walls, y_walls) in finish:          # checking if pointer have finished the maze
            print("Finished")
            endProgram()
        if (x_walls +24, y_walls) in walls:          # checking  if they are walls on the left
            if(x_walls, y_walls -24) not in walls:   # checking if path ahead is clear
                self.forward(24)
                steps = steps + 1
            else:
                self.right(90)
        else:
            self.left(90)
            self.forward(24)

全局步骤
插入为步骤指定新值的每个函数的第一行。这让Python知道您的意思是修改全局变量,而不是设置恰好具有相同名称的局部变量。

插入
全局步骤
,作为每个函数的第一行,为
步骤
指定新值。这让Python知道您的意思是修改全局变量,而不是设置恰好具有相同名称的局部变量。

步骤可以被引用,但不能更改,除非您声明它为
全局
步骤
可以被引用,但不能更改,除非您声明它为
全局