For my tic-tac-toe program, I decided to create a function that checks whether a player's input is available or not. I defined a function named checkOverride()
that checks for the input of a player. If the input has already been chosen and has either an "X"
or an "O"
the function would return True
. Else it would return False
. However, when testing the program I attempted to make the function return True
, but the value the function returned was None
. Why is this?
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