井字游戏代码有什么问题?

时间:2019-06-23 22:20:06

标签: python debugging

我正在尝试为Python制作一个Tic-Tac-Toe游戏,并且游戏板在应该改变的时候没有改变。

我已经验证了if语句中的代码正在运行,所以我知道这不是问题。

#defines the Tic-Tac-Toe board (dictionary)
board={'L1':' ','M1':' ','R1':' ',
       'L2':' ','M2':' ','R2':' ',
       'L3':' ','M3':' ','R3':' '}
#if the computer or the player are about to get three in a row (this is only one of the many combinations I added)
if board['L1']==board['L2']!=' ' and board['L3']==' ':
    #x1 is a variable that determines if the player is X's or O's (first or second)
    if x1==1:
        board['L3']=='O'
        else:
            board['L3']=='X'
        #board
        played=1

这是我完整代码的链接:https://repl.it/repls/BruisedBusyGraph(尝试以2级难度运行它)

我希望代码在运行后将其L3属性更改为X或O,但是即使在运行if语句后,它也保持空白。

1 个答案:

答案 0 :(得分:0)

问题出在这部分:

if x1==1:
    board['L3']=='O'
    else:
        board['L3']=='X'

您正在混合使用相等性测试==和赋值运算符=

更改以下内容应该可以:

if x1==1:
    board['L3']='O'
    else:
        board['L3']='X'