[英]Local variable referenced before assignment
大家好,我是数据结构的新手,我一直在尝试使用左手规则算法解决迷宫问题。 但是,由于我一直面临此错误,因此我在尝试打印出解决迷宫所需的步骤数时遇到了困难:
UnboundLocalError: local variable 'steps' referenced before assignment
我试图初始化一个步骤变量无济于事。
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)
将global steps
作为为steps
分配新值的每个函数的第一行插入。 这让 Python 知道您的意思是修改全局变量,而不是设置恰好具有相同名称的局部变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.