0
我寫了對AI這個井字遊戲(我提高了AI現在):NameError:全局名稱「Player1_row」沒有定義
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阻止玩家的最後一招。
函數'turnX()'中的變量'Player1_row'是本地的。它在'turnX()'返回時立即被銷燬,並且在'turnY()'中不可訪問。你可能想在'turnX()'中加入'global Player1_row'。 – DyZ
Python給你一個堆棧跟蹤顯示你哪一行有問題。請張貼整個跟蹤。 Python告訴你,但是你希望我們挖掘你的代碼來找出問題所在?! – tdelaney