2017-12-09 14 views
-1
A='A' 
B='B' 
C='C' 
D='D' 
E='E' 
F='F' 
G='G' 
H='H' 
I='I' 
list1=[A,B,C] 
list2=[D,E,F] 
list3=[G,H,I] 

def Output(): 
    print(list1) 
    print(list2) 
    print(list3) 

Output() 

print('The object of Tic Tac Toe is to get three in a row. You play on a three by three game board. The first player is known as X and the second is O. Players alternate placing Xs and Os on the game board until either oppent has three in a row or all nine squares are filled. X always goes first, and in the event that no one has three in a row, the stalemate is called a cat game.') 
print() 
print('This is a two player game. Single-player Coming soon!') 
print() 
print('You must place your letter by choosing a letter from above to decide the co-ordinates. The first player starts with X, and the second player starts with O.') 

Decision=input('Enter Co-ordinate of First player:') 
if Decision == A: 
    A ='X' 
if Decision == B: 
    B = 'X' 
if Decision == C: 
    C ='X' 
if Decision == D: 
    D ='X' 
if Decision == E: 
    E ='X' 
if Decision == F: 
    F ='X' 
if Decision == G: 
    G ='X' 
if Decision == H: 
    H ='X' 
if Decision == I: 
    I ='X' 
Output() 

これは私がTic Tac Toe用に書いたコードです。私が座標に入り、出力関数を使用するたびに、文字Xは入力された座標の場所に表示されません。変数で値が置き換えられないのはなぜですか?

+3

おそらく座標にXを割り当てるためのもの、例えば、 ' A = 'X'ではなく、A = 'X'である。 – Reti43

+0

Vivek、@ Reti43で言及されているバグを修正するために質問を編集したことがあります。あなたの問題を解決したか/あなたの質問に答えましたか? – SherylHohman

答えて

1

A'A'への参照であり、list1[0]ではありません。 list1の最初の値をAと初期化しているからといって、変数Aの変更がリストの値に影響するわけではありません。また、変数割り当てに==を使用します。 ==は同等のチェッカーです。ここでは、代わりに何をすべきかです:

if decision == 'A': 
    list1[0] = 'X' 

この方法は、リスト内の値が変更され、あなたがそのようなA = 'A'B = 'B'として変数の割り当てのいずれかを必要としない、など

1

ときので、あなたはそうです:

if Decision == A: 
    A ='X' 

はい、あなたは値を変更していますが、変更されたものはどこに保存していますか?

そのために、あなたはそのインデックスで、そのために変更するには、リストを伝える必要があります:

if Decision == A: 
    list1[index_no] = 'value' 

をそれほど:

if Decision == A: 
     list1[0] = 'X' 
関連する問題