首页 > 解决方案 > 井字游戏的错误输出

问题描述

我无法更正此代码的输出:


    print('--------')
    print('|'+board[7]+ '|' +board[8] +'|' +board[9]+'|')
    print('--------')
    print('|' +board[4]+ '|'+board[5]+'|' +board[6]+'|')
    print('--------')
    print('|' +board[1]+ '|'+board[2]+'|' +board[3]+'|')
    print('--------')

real_board=['#','#','#','#','#','#','#','#','#','#']
def xchecker_tool(board, mark):
    vertical=print(f'congratulations player {mark}! you have won by lining up vertically on the board!')  
    horizontal=print(f'congratulations player {mark}! you have won by lining up horizontally on the board!')
    diagonal= print(f'congratulations player {mark}! you have won by lining up diagonally!')

    if board[1]==mark and board[2]==mark and board[3]==mark: 
        horizontal
    elif board[4]==mark and board[5]==mark and board[6]==mark:
         horizontal
    elif board[7]==mark and board[8]==mark and board[9]==mark:
        horizontal

    if board[1]==mark and board[4]==mark and board[7]==mark:
        vertical
    elif board[2]==mark and board[5]==mark and board[8]==mark:
            vertical
    elif board[3]==mark and board[6]==mark and board[9]==mark:
        vertical

    if board[1]==mark and board[5]==mark and board[9]==mark:
            diagonal
    elif board[3]==mark and board[5]==mark and board[7]==mark:
         diagonal
def game_rounds(round):
    player_moves=int(input('choose a number from 1 to 9: '))
    while True:
        if (player_moves)<1 or (player_moves)>9:
            print('unacceptable range')
        elif (player_moves)==1:
            real_board[1]='X'
        elif (player_moves)==2:
            real_board[2]='X'
        elif (player_moves)==3:
            real_board[3]='X'
        elif (player_moves)==4:
            real_board[4]='X'
        elif (player_moves)==5: 
            real_board[5]='X'
        elif (player_moves)==6:
            real_board[6]='X'           
        elif (player_moves)==7:
            real_board[7]='X'
        elif (player_moves)==8:
            real_board[8]='X'
        else:
            real_board[9]='X'
        break
    display_board(real_board)

def y_rounds(round):
    y_moves=int(input('choose a number from 1 to 9: '))

    while True:
        if (y_moves)<1 or (y_moves)>9:
            print('unacceptable range')
        elif (y_moves)==1:
            real_board[1]='O'
        elif (y_moves)==2:
            real_board[2]='O'
        elif (y_moves)==3:
            real_board[3]='O'
        elif (y_moves)==4:
            real_board[4]='O'
        elif (y_moves)==5: 
            real_board[5]='O'
        elif (y_moves)==6:
            real_board[6]='O'           
        elif (y_moves)==7:
            real_board[7]='O'
        elif (y_moves)==8:
            real_board[8]='O'
        else:
            real_board[9]='O'
        break
    display_board(real_board)
def run_game(full):
    game_on=True
    game_off=False
    t=0    
    while game_on:
            game_rounds(1)
            t+=1
            if t==5:
                break
            y_rounds(1)
            xchecker_tool(real_board, 'X')
            xchecker_tool(real_board, 'O')


run_game(1)

我得到这个输出。据我了解,xchecker_tool 函数定义不正确,这就是为什么即使没有排列“x”或“o”,它也会每两圈打印一次“恭喜”消息。我还希望帮助清理脚本,因为它非常冗长,但由于经常出现错误,我目前的水平无法做到这一点。

choose a number from 1 to 9: 3
--------
|#|#|#|
--------
|#|#|#|
--------
|#|#|X|
--------
choose a number from 1 to 9: 5
--------
|#|#|#|
--------
|#|O|#|
--------
|#|#|X|
--------
congratulations player X! you have won by lining up vertically on the board!
congratulations player X! you have won by lining up horizontally on the board!
congratulations player X! you have won by lining up diagonally!
congratulations player O! you have won by lining up vertically on the board!
congratulations player O! you have won by lining up horizontally on the board!
congratulations player O! you have won by lining up diagonally!

标签: pythonpython-3.xtic-tac-toe

解决方案


这里:

vertical=print(f'congratulations player {mark}! you have won by lining up vertically on the board!')  
horizontal=print(f'congratulations player {mark}! you have won by lining up horizontally on the board!')
diagonal= print(f'congratulations player {mark}! you have won by lining up diagonally!')

print将被执行,其返回值将绑定到变量。您定义了三个变量 - 因此print将执行三次,将文本输出到终端。

像这样的代码:

if board[1]==mark and board[2]==mark and board[3]==mark: 
        horizontal

仅仅“提及”变量horizontal,它并没有真正执行任何事情。你也可以这样写:

if board[1]==mark and board[2]==mark and board[3]==mark: 
    None  # won't do anything
    ...   # won't do anything either
    print
    board[1]

这些都不会执行任何操作。

您应该在if语句中打印:

if board[1]==mark and board[2]==mark and board[3]==mark: 
    print(f'congratulations player {mark}! you have won by lining up horizontally on the board!')
# and so on for the other `if`s

推荐阅读