我在python中输入了一个变量,但不会被接受。 我不确定为什么会这样。我试图更改变量的名称,更改名称的大小写以及更改函数的输入键。 我的代码是:
import turtle
startup = 1
screen = turtle.Screen()
screen.bgcolor("black")
screen.bgpic("Assets/Title.png")
screen.title("Game")
def startgame():
if startup == 1:
screen.bgpic("Assets/Title_Two.png")
startup = 2
turtle.listen()
turtle.onkey(startgame, "Left")
我得到的唯一错误消息是:
>>> Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files (x86)\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Program Files (x86)\Python37-32\lib\turtle.py", line 686, in eventfun
fun()
File "C:\Users\Adam\OneDrive\Programming Stuff\Game\Game.py", line 9, in startgame
if startup == 1:
UnboundLocalError: local variable 'startup' referenced before assignment
答案 0 :(得分:0)
将启动转换为全局变量:
import turtle
startup = 1
screen = turtle.Screen()
screen.bgcolor("black")
screen.bgpic("Assets/Title.png")
screen.title("Game")
def startgame():
global startup
if startup == 1:
screen.bgpic("Assets/Title_Two.png")
startup = 2
turtle.listen()
turtle.onkey(startgame, "Left")