为什么我的函数返回none而不是true?

jtw3ybtb  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(286)

对于我的tic-tac-toe程序,我决定创建一个函数来检查玩家的输入是否可用。我定义了一个名为 checkOverride() 检查玩家的输入。如果已经选择了输入并且 "X" 或者一个 "O" 函数将返回 True . 否则它会回来的 False . 但是,在测试程序时,我试图使函数返回 True ,但函数返回的值为 None . 为什么会这样?

def main():
    # Welcome the players
    print("Welcome players to Tic-Tac-Toe!")

    # Variables for creating game board
    top     = "1|2|3"
    middle  = "4|5|6"
    bottom  = "7|8|9"
    divider = "-+-+-"

    # Display game board
    Board = createGameBoard(top, middle, bottom, divider)
    print(Board)

    playerOneWin = False
    playerTwoWin = False

    while playerOneWin == False and playerTwoWin == False:
        # Get input from player One
        playerOneInput = input("x's turn to choose a square (1-9): ")

        # Check for override
        override = checkOverride(Board, playerOneInput)

        if override == True:
            print(f"{playerOneInput} has already been filled. Please choose another square.")
            continue

        elif override == False:
            Board = playerOneMove(Board, playerOneInput)
            print(Board)

        # Get input from player Two
        playerTwoInput = input("o's' turn to choose a square(1-9): ")

        # Check for override
        override = checkOverride(Board, playerTwoInput)

        if override == True:
            print(f"{playerTwoInput} has already been filled. Please choose another square.")
            continue

        elif override == False:
            Board = playerTwoMove(Board, playerTwoInput)
            print(Board)

def createGameBoard(top, middle, bottom, divider):
    ''' 
    Create a Tic-Tac-Toe game board.
    '''
    Board = f"{top}\n{divider}\n{middle}\n{divider}\n{bottom}"
    return Board

def playerOneMove(Board, playerOneInput):
    '''
    Take player one's input and display it onto the board.
    '''
    # Identify where player one's move and replace number with 'X'
    if playerOneInput in Board:
        Board = Board.replace(playerOneInput, 'X')

    return Board

def playerTwoMove(Board, playerTwoInput):
    '''
    Take player two's input and display it onto the board.
    '''
    # Identify where player one's move and replace number with 'X'
    if playerTwoInput in Board:
        Board = Board.replace(playerTwoInput, "O")

    return Board

def checkOverride(Board, playerOneInput="", playerTwoInput=""):
    '''
    Look at the input of player one or player two and see if their input is already taken.
    '''

    if playerOneInput:
        if playerOneInput in Board:
            square = Board.index(playerOneInput)
            if Board[square] == "X" or Board[square] == "O":    
                return True
            else:
                return False

    elif playerTwoInput:
        if playerTwoInput in Board:
            square = Board.index(playerTwoInput)
            if Board[square] == "X" or Board[square] == "O":
                return True
            else:
                return False
k10s72fa

k10s72fa1#

函数返回的唯一方法 None 如果两者都有 playerOneInput 以及 playerTwoInput 等于 "" , False ,或 None (因为他们会不满足条件)或者两者兼而有之 playerOneInput 以及 playerTwoInput 在电路板中找不到(如果是这种情况,那么我们也无能为力,因为您没有向我们提供电路板的代码,以便我们可以看到问题所在)。
因此,函数返回的原因 None 是因为运行它时没有参数,使其默认为值 "" 为了 playerOneInput 以及 playerTwoInput .

7y4bm7vi

7y4bm7vi2#

一般函数返回 None 如果他们没有达成协议 return 有价值的。
在您的功能中,这可以通过两种方式实现:
两者 playerOneInput 以及 playerTwoInput 都是假的。当这种情况发生时,代码执行将在以下两种情况下结束 if 块,其中没有更多代码可执行,以及 None 会被归还,因为没有什么可以归还(为了理解“truthy”和“falsy”的意思,这个答案解释得很好。)
如果其中一个 playerOneInput 以及 playerTwoInput 是真实的,但当检验它是否真实 in Board ,其计算结果为 False . 根本没有 else: 为了捕捉执行,它最终没有什么可返回的,因此它返回 None .
要修复2,您可能需要更改

if playerOneInput:
        if playerOneInput in Board:

if playerOneInput and playerOneInput in Board:

如果你们两个都需要的话 True 当玩家的输入是真实的,而不是在棋盘上时,你不想用不同的方式来处理它。
要修复1,您需要弄清楚是否要在开始时测试输入,并在两者都不存在时引发错误 playerOneInput 或者 playerTwoInput (或者甚至 Board )是可用的,或者如果您想在 else: 在函数的末尾。
另外,您可能只需要一个播放器的输入,然后调用函数两次。

相关问题