UnboundLocalError: невозможно получить доступ к локальной переменной currentPlayer, если она не связана со значением.Python

Программы на Python
Ответить Пред. темаСлед. тема
Гость
 UnboundLocalError: невозможно получить доступ к локальной переменной currentPlayer, если она не связана со значением.

Сообщение Гость »


Я не понимаю эту ошибку. '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()

I tried passing currentPlayer by value but then the TogglePlayer() method only updates the local variable.


Источник: https://stackoverflow.com/questions/781 ... -it-is-not
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»