这个TicTacToe游戏代码出了什么问题?

2024-05-16 05:04:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为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级难度运行它)

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


Tags: orthe代码boardl1ifis语句
1条回答
网友
1楼 · 发布于 2024-05-16 05:04:41

问题来自这一部分:

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

您混合了相等测试==和赋值运算符=

用以下方法更改此零件应有效:

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

相关问题 更多 >