これは簡単なtic-tac-toe割り当てのために作成した関数です。変数Turn
の正しい番号を返すようには見えませんが、IPythonの出力に正しい値が表示されます。誰でも助けてくれますか?関数から変数を取り出すことができません
#INITIALIZE VARIABLES
CELLS = [1,2,3,4,5,6,7,8,9]
Taken_position = []
X_positions = []
O_positions = []
Computer_player = ""
Turn = 0 # turn 1 is X
def play_game(Computer_pla yer, Taken_position, X_positions, O_positions,
CELLS, Turn):
#print("enter")
if Computer_player == 'X':
#Turn % 2 != 0: # This is player x
Turn += 1
play_position = input("Player X : What square do you want to play?")
test_num = (int(play_position))
while test_num in Taken_position:
play_position = input("Player X : That square is taken!! Try again?")
add_num = (int(play_position)-1)
Taken_position.append(play_position) # keep track of all position played
X_positions.append(play_position) # Keep track of X player, test winner
CELLS[add_num] = 'X'
else: # this is O player
Turn += 1
play_position = input("Player O : What square do you want to play?")
test_num2 = (int(play_position))
while test_num2 in Taken_position:
play_position = input("Player O : That square is taken!! Try again?")
add_num = (int(play_position)-1)
Taken_position.append(play_position) # keep track of all position played
O_positions.append(play_position) # Keep track of o player, test winner
CELLS[add_num] = 'O'
return Taken_position, X_positions, O_positions, CELLS, Turn
#RUN FUNCTION
play_game(Computer_player, Taken_position, X_positions, O_positions, CELLS, Turn)
これは私がIPythonから得た出力です。正しい出力が表示されますが、機能を終了するときにはTurn
変数が増加していないようです。
Player O : What square do you want to play? 2
Out[318]: (['2'], [], ['2'], [1, 'O', 3, 4, 5, 6, 7, 8, 9], 1)
これは、私たちがデバッグしたいコードのかなりの部分です。問題をより具体的にすることができます。 –
インデントを修正して、コードから質問を取り除いてください。 – roganjosh
なぜ関数の外側で「ターン」インクリメントする必要がありますか?私はあなたが関数を一度呼び出すと、すべてがその中に含まれていると思いますか? '#RUN FUNCTION'の中で' Turn = play_game(Computer_player、Taken_position、X_positions、O_positions、CELLS、 ターン)[4] 'のようなものを試すことができます。 – roganjosh