python - 赋值前引用的局部变量
问题描述
大家好,我是数据结构的新手,我一直在尝试使用左手规则算法解决迷宫。但是,当我一直面临这个错误时,我在尝试打印出解决迷宫所需的步骤数时遇到了困难:
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 知道您的意思是修改全局变量,而不是设置恰好具有相同名称的局部变量。
推荐阅读
- c# - 在 asp.net core 2.2 上上传文件 API 时无法使用替换功能
- c# - 如何将非一次性对象与冷可观察对象的每个订阅绑定?
- nuxt.js - 在加载 nuxt.config.js 'css' 之前在运行时加载 scss 变量
- python - 在 Pandas 中计算滚动 beta
- vba - 如何将文本附加到文本文件?
- r - R中二分法的矢量化IF
- database-design - 如何处理微服务中的公共数据
- python - BeautifulSoup AttributeError: NoneType
- node.js - Heroku 在尝试打开我的网络应用程序时给了我一个 503,但它在我的本地主机上运行良好
- node.js - 如何使用 sequelize 和 typescript 创建模型?