-4

I entered a variable in python, but it will not be accepted. I'm not sure why this is happening. I have tried to change the name of the variable, changing capitalization in the name of it, and changing the input key for the function. My code is:

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")  

The only error message I've gotten is:

>>> 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
  • I presume your question is downvoted (twice) due to its vague nature. Remember, we know nothing of your scenario. Please provide an example so we can help. – S3DEV May 23 '20 at 22:04
  • I've just run your revised code and can't generate the error you've posted. What do you mean by 'it will not be accepted' exactly? Without knowing more details, I'm wondering if your issue is just that you're trying to set a value for `startup` inside a function that won't be available outside the function. [This answer](https://stackoverflow.com/a/423668/7508700) might help, if that is the case. – David Buck May 24 '20 at 09:19

1 Answers1

0

Turn startup into a global variable:

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")  
Red
  • 26,798
  • 7
  • 36
  • 58