为什么不在变量中替换Value?

时间:2017-12-09 02:40:14

标签: python

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就不会出现在输入的坐标位置。

2 个答案:

答案 0 :(得分:1)

A是对'A'的引用,而不是list1[0]。仅仅因为您使用第一个值list1初始化A并不意味着变量A中的更改会影响列表中的值。此外,您使用==进行变量赋值。 ==是一个相等检查器。这是你应该做的事情:

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

这样,列表中的值将会更改,您不需要任何变量赋值,例如A = 'A'B = 'B'等。

答案 1 :(得分:1)

因为当你这样做时:

if Decision == A:
    A ='X'

是的,您正在更改值,但是您在哪里存储更改的内容?

为此,您必须告诉列表在该索引处更改并为此:

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

所以:

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