Python不会执行超过第一个语句的任何elif语句

时间:2018-08-01 03:12:45

标签: python python-3.x

我正在编写基本的Connect 4游戏,并且将面板内置在命令行中,等等。我的问题是我无法通过第二条elif语句执行代码。我将其设置为如果网格中的某个单元格没有下划线,则应继续将其放置在下一行中。但是,以下操作始终仅替换第2行单元格中的任何内容。我试图从除底部2行之外的其他行开始,以尝试进行故障排除,但它始终不会超过第一个elif语句。谁能告诉我我的Elif错在哪里?

board = []

for x in range(0, 6):
    board.append(["_"] * 7)

def print_board(board):
    for i in range(1,7):
        print(i, end=" ")
    print(7)
    for row in board:
        print("|".join(row))

print_board(board)

for turn in range(42):
    print('Turn', turn+1)
    if turn % 2 == 0:
        player1 = int(input('Player 1, choose your column: '))
        while player1 not in range(1,8):
            player1 = int(input('You must enter a column number from 1-7: '))
        if board[5][player1-1] == '_':
            board[5][player1-1] = 'O'
        elif board[5][player1-1] != '_':
            board[4][player1-1] = 'O'
        elif board[4][player1-1] != '_':
            board[3][player1-1] = 'O'
        elif board[3][player1-1] != '_':
            board[2][player1-1] = 'O'
        elif board[2][player1-1] != '_':
            board[1][player1-1] = 'O'
        elif board[1][player1-1] != '_':
            board[0][player1-1] = 'O'
        print_board(board)
    elif turn % 2 != 0:
        player2 = int(input('Player 2, choose your column: '))
        while player2 not in range(1,8):
            player2 = int(input('You must enter a column number from 1-7: '))
        if board[5][player2-1] == '_':
            board[5][player2-1] = 'X'
        elif board[5][player2-1] != '_':
            board[4][player2-1] = 'X'
        elif board[4][player2-1] != '_':
            board[3][player2-1] = 'X'
        elif board[3][player2-1] != '_':
            board[2][player2-1] = 'X'
        elif board[2][player2-1] != '_':
            board[1][player2-1] = 'X'
        elif board[1][player2-1] != '_':
            board[0][player2-1] = 'X'
        print_board(board)

1 个答案:

答案 0 :(得分:0)

您应该使用==而不是!=测试单元格。另外,您应该更改要测试的单元格,而不是其下面的单元格:

board = []

for x in range(0, 6):
    board.append(["_"] * 7)

def print_board(board):
    for i in range(1,7):
        print(i, end=" ")
    print(7)
    for row in board:
        print("|".join(row))

print_board(board)

for turn in range(42):
    print('Turn', turn+1)
    if turn % 2 == 0:
        player1 = int(input('Player 1, choose your column: '))
        while player1 not in range(1,8):
            player1 = int(input('You must enter a column number from 1-7: '))
        if board[5][player1-1] == '_':
            board[5][player1-1] = 'O'
        elif board[4][player1-1] == '_':
            board[4][player1-1] = 'O'
        elif board[3][player1-1] == '_':
            board[3][player1-1] = 'O'
        elif board[2][player1-1] == '_':
            board[2][player1-1] = 'O'
        elif board[1][player1-1] == '_':
            board[1][player1-1] = 'O'
        elif board[0][player1-1] == '_':
            board[0][player1-1] = 'O'
        print_board(board)
    elif turn % 2 != 0:
        player2 = int(input('Player 2, choose your column: '))
        while player2 not in range(1,8):
            player2 = int(input('You must enter a column number from 1-7: '))
        if board[5][player2-1] == '_':
            board[5][player2-1] = 'X'
        elif board[4][player2-1] == '_':
            board[4][player2-1] = 'X'
        elif board[3][player2-1] == '_':
            board[3][player2-1] = 'X'
        elif board[2][player2-1] == '_':
            board[2][player2-1] = 'X'
        elif board[1][player2-1] == '_':
            board[1][player2-1] = 'X'
        elif board[0][player2-1] == '_':
            board[0][player2-1] = 'X'
        print_board(board)