一个Tic-Tac-T游戏的列表值替换问题

2024-10-03 09:16:14 发布

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

这是我目前的代码:

row1 = ["","",""]
row2 = ["","",""]
row3 = ["","",""]

def gameTime ()
    print ( row1[0], row1[1], row1[2] )
    print ( row2[0], row2[1], row2[2] )
    print ( row3[0], row3[1], row3[2] )

while True:
    player1Row = input ( "Select a row: " )
    player1Column = input ( "Select a column: " )

    if player1Row == 1 and player1Column == 1:
        if row1[0] !="X" and row1[0] !="O":
            row1 [0] = "X"

    gameTime ()

我尝试了几种不同的策略,使用.append()甚至.remove(),然后使用.append()替换列表索引项[0],但没有效果。我觉得我遗漏了一些东西,因为如果我将列表项的值设置为0,1,2而不是“”的话,它是有效的,但我希望现在就这样做,以便完成游戏。如有任何建议,我们将不胜感激。谢谢你的时间!你知道吗


Tags: and代码列表inputifdefselectrow1
1条回答
网友
1楼 · 发布于 2024-10-03 09:16:14

player1Rowplayer1Column属于str类型。你知道吗

但是,列表索引应该是int而不是str类型的

需要将输入的返回转换为int

player1Row    = int(input ( "Select a row: " ))
player1Column = int(input ( "Select a column: " ))

相关问题 更多 >