Я не понимаю эту ошибку. 'currentPlayer' объявлен и определен в строке 13, которая существует глобально. Почему в моем методе ToggleTurn() он не привязан?
Ошибка:
Код: Выделить всё
Traceback (most recent call last):
File "...main.py", line 83, in
PlacePiece(currentPlayer,5)
File "...main.py", line 51, in PlacePiece
ToggleTurn()
File "...main.py", line 28, in ToggleTurn
if currentPlayer == 1:
^^^^^^^^^^^^^
UnboundLocalError: cannot access local variable 'currentPlayer' where it is not associated with a value
Код: Выделить всё
import pygame as pg
pg.init()
currentPlayer = 2 # 1 = o and 2 = x
def ToggleTurn():
if currentPlayer == 1:
currentPlayer = 2
elif currentPlayer == 2:
currentPlayer = 1
else:
print("ERROR: Could not progress to the next turn!")
return False
return True
def PlacePiece(piece,slot):
if not (1,slot) in pieceList or not (2,slot) in pieceList:
pieceList.append((piece,slot))
ToggleTurn()
else:
print("GAME NOTE: A piece has already been placed there!")
RUN = True
while RUN:
#event handler
for event in pg.event.get():
if event.type == pg.QUIT:
RUN = False
if event.type == pg.KEYDOWN:
if event.key == pg.K_KP1:
PlacePiece(currentPlayer,1)
#elif for the other numpad buttons
pg.display.update()
Источник: https://stackoverflow.com/questions/781 ... -it-is-not