NameError: global name 'Player1_row' is not defined
NameError: global name 'Player1_row' is not defined
我针对 AI 编写了这个井字游戏(我现在正在改进 AI):
Matrix = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
Matrix_2 = [[" ", " ", " "],
[" ", " ", " "],
[" ", " ", " "]]
def turnX():
Player1_row = int(raw_input("P1 What row do you want?"))
Player1_row = Player1_row - 1
Player1_column = int(raw_input("P1 What column do you want?"))
Player1_column = Player1_column - 1
if Player1_row > Matrix:
turnX()
if Player1_column > Matrix:
turnX()
if (1 == Matrix[Player1_column][Player1_row] or 500 == Matrix[Player1_column][Player1_row]):
print "This is an invaild move!"
turnX()
else:
Matrix[Player1_column][Player1_row] = 1
Matrix_2[Player1_column][Player1_row] = "X"
def turnY():
global Player1_row
Player2_row = int(random.randint(1, boardX))
Player2_row = Player2_row - 1
Player2_column = int(random.randint(Player1_row, boardY))
Player2_column = Player2_column - 1
if (1 == Matrix[Player2_column][Player2_row] or 500 == Matrix[Player2_column][Player2_row]):
turnY()
else:
print "AI Turn:"
Matrix[Player2_column][Player2_row] = 500
Matrix_2[Player2_column][Player2_row] = "O"
但是我收到这个错误:
NameError: global name 'Player1_row' is not defined`.
我正在尝试让 AI 阻止玩家的最后一步。
Player1_row不是全局变量,而是函数turn_X.
的局部变量
要解决此问题,请尝试将 Player1_row 设置为 turn_X 中的全局变量。
我针对 AI 编写了这个井字游戏(我现在正在改进 AI):
Matrix = [[0, 0, 0],
[0, 0, 0],
[0, 0, 0]]
Matrix_2 = [[" ", " ", " "],
[" ", " ", " "],
[" ", " ", " "]]
def turnX():
Player1_row = int(raw_input("P1 What row do you want?"))
Player1_row = Player1_row - 1
Player1_column = int(raw_input("P1 What column do you want?"))
Player1_column = Player1_column - 1
if Player1_row > Matrix:
turnX()
if Player1_column > Matrix:
turnX()
if (1 == Matrix[Player1_column][Player1_row] or 500 == Matrix[Player1_column][Player1_row]):
print "This is an invaild move!"
turnX()
else:
Matrix[Player1_column][Player1_row] = 1
Matrix_2[Player1_column][Player1_row] = "X"
def turnY():
global Player1_row
Player2_row = int(random.randint(1, boardX))
Player2_row = Player2_row - 1
Player2_column = int(random.randint(Player1_row, boardY))
Player2_column = Player2_column - 1
if (1 == Matrix[Player2_column][Player2_row] or 500 == Matrix[Player2_column][Player2_row]):
turnY()
else:
print "AI Turn:"
Matrix[Player2_column][Player2_row] = 500
Matrix_2[Player2_column][Player2_row] = "O"
但是我收到这个错误:
NameError: global name 'Player1_row' is not defined`.
我正在尝试让 AI 阻止玩家的最后一步。
Player1_row不是全局变量,而是函数turn_X.
的局部变量要解决此问题,请尝试将 Player1_row 设置为 turn_X 中的全局变量。