[Python] How does this code to create a board for a Tic-Tac-Toe game work?

Discussão em 'Python' iniciado por Stack, Março 16, 2025.

  1. Stack

    Stack Membro Participativo

    After I couldn't figure out, how to build a board for a Tic-Tac-Toe game by myself, I randomly discoverd an already finished Tic-Tac-Toe program from another programmer.

    What she did was:

    board = [' ' for _ in range(9)]

    def print_board():
    for row in [board[i*3:(i+1)*3] for i in range(3)]:
    print('| ' + ' | '.join(row) + ' |')

    print_board()


    Output:

    | | | |

    | | | |

    | | | |


    Now I am totally confused with this sector of the code:

    board[i*3:(i+1)*3]


    What does this part of the code mean and why is it so essential to use it in this case?

    If you want the whole context of the program or try it yourself: https://github.com/kying18/tic-tac-toe/blob/master/game.py

    Continue reading...

Compartilhe esta Página