简体   繁体   English

如何使用用户输入修改列表(二维数组)? (Python)

[英]How to modify a list (2D Array) using user input? (Python)

I am trying to make a basic tic-tac-toe game, so I need the user to input which part of the grid they would like to change into an "X" or "O" (required to use lists).我正在尝试制作一个基本的井字游戏,所以我需要用户输入他们想要将网格的哪一部分更改为“X”或“O”(需要使用列表)。 The grid can be accessed using the syntax board[row][column].可以使用语法 board[row][column] 访问网格。

board = [["1", "2", "3"],
    ["4", "X", "6"],
    ["7", "8", "9"]]

def EnterMove(board):
    player_move = input("Using board[row][column] syntax, make your move: ")
    if player_move in board:      # program doesn't recognize the input here
            board[player_move] = "O"

I don't think is the best practice, but我不认为是最好的做法,但是

for row in board:
        if player_move in row:      
                row[row.index(player_move)] = "O"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM