[英]Why does my function return None instead of True?
對於我的井字游戲程序,我決定創建一個 function 來檢查玩家的輸入是否可用。 我定義了一個名為checkOverride()
的 function 來檢查玩家的輸入。 如果輸入已經被選擇並且具有"X"
或"O"
,則 function 將返回True
。 否則它將返回False
。 但是,在測試程序時,我試圖讓 function 返回True
,但 function 返回的值是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
function 返回None
的唯一方法是,如果playerOneInput
和playerTwoInput
都等於""
、 False
或None
(因為它們會不符合條件)或者playerOneInput
和playerTwoInput
在 Board 中都找不到(如果這是這種情況,那么我們無能為力,因為您尚未向我們提供 Board 的代碼,以便我們查看問題所在)。
因此, function 返回None
的原因是因為您在沒有參數的情況下運行它,因此playerOneInput
和playerTwoInput
的默認值為""
。
通常,如果函數在沒有return
值的情況下結束,則返回None
。
在您的 function 中,這可能以兩種方式發生:
playerOneInput
和playerTwoInput
都是虛假的。 發生這種情況時,代碼執行將在兩個if
塊下方結束,沒有更多代碼可以執行,並且None
將被返回,因為沒有任何東西可以返回。 (要了解“真實”和“虛假”的含義,這個 SO 答案解釋得很好。)playerOneInput
和playerTwoInput
之一是真實的,也會發生同樣的事情,但是在測試它是否in Board
時,這會評估為False
。 沒有else:
present 來捕獲執行,它最終沒有什么可以返回,因此它返回None
。要修復 2,您可能需要更改
if playerOneInput:
if playerOneInput in Board:
至
if playerOneInput and playerOneInput in Board:
如果您需要兩者都為True
,並且您不想在玩家輸入為真但不在棋盤中時以不同方式處理它。
要修復 1,您需要確定是否要在開始時測試輸入,並在playerOneInput
或playerTwoInput
(甚至可能是Board
)都不可用時引發錯誤,或者如果您想在else:
function 的末端。
此外,您可能只想接受一名玩家的輸入,然后調用 function 兩次。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.