Reputation: 27
I am trying to make a hangman game with pygame. The game itself is irrelevant for this. I defined a variable early on in my code, then later, when a function is called that uses the variable, Python returns a "UnboundLocalError: local variable 'guessedLetters' referenced before assignment" Both the function and the call are later in my code than the variable assignment. This is the variable assignment:
guessedLetters = ""
.
This is the function:
def guess(letter):
if letter in word:
pass
else:
guessedLetters += ' ' + letter
hangmanStage += 1
This is where it is called(which is within another method):
def redrawWindow():
win.fill((150, 150, 150))
drawLetters()
drawGuessedLetters()
drawHangman()
And this is where that method is called
while running:
for event in ...
redrawWindow()
pygame.display.update()
Upvotes: 2
Views: 88
Reputation: 15470
guessedLetters
should be declared inside the function if use is local or outside if it is global.
In first case, if it is declared outside function, add a global guessedLetters
before referencing to it.
If it is local then add guessedLetters = ''
on top.
Do this for other variables like hangmanStage
too.
Upvotes: 1