Я начинающий программист, изучающий, как использовать Python и пишу свой первый код ti c -ta c -ee.
FirstQ = input("Welcome to Tic Tac Toe!" + "\n" + "Player 1: Do you want to be X or O? ")
def place_marker(): #This function will now attempt to place an X onto the board
if FirstQ == "X":
player1 = FirstQ
player2 = "O"
elif FirstQ == "O":
player1 == FirstQ
player2 == "X"
Так что я пытаюсь сделать это как только игрок выбирает, X или O. Я пытаюсь назначить это игроку.
Когда я набираю X, код работает нормально, но если я набираю OI, появляется следующая ошибка:
Traceback (most recent call last):
File "D:/Python/Projects/TIC TAC TOE.py", line 46, in <module>
place_marker()
File "D:/Python/Projects/TIC TAC TOE.py", line 36, in place_marker
UnboundLocalError: local variable 'player1' referenced before assignment
Может кто-нибудь объяснить, как это работает?
Вот весь код, который я сделал до сих пор:
#Global Variables
testboard = ([' ',' ',' ',' ',' ',' ',' ',' ',' '])
player1 = input("Welcome to Tic Tac Toe!" + "\n" + "Player 1: Do you want to be X or O? ")
def display_board(board): #Function for designing the board
print (' | | ')
print ('{} | {} | {} '.format(board[6],board[7],board[8])) #This is where the X and O will go
print (" | | ")
print ("-----------")
print (" | | ")
print ('{} | {} | {} '.format(board[3],board[4],board[5])) #This is where the X and O will go
print (" | | ")
print ("-----------")
print (" | | ")
print ('{} | {} | {} '.format(board[0],board[1],board[2])) #This is where the X and O will go
print (" | | ")
pass
def player_input(): #Function for assigning the player whether they will be X or O
if player1 == "X":
return ("Player 1 will start first as X")
elif player1 == "O":
return ("Player 1 will start first as O")
# else:
#return ("Sorry, please enter in either X or O" ||Work on this Later
#print (player_input())
def place_marker(): #This function will now attempt to place an X onto the board
if player1 == "X":
player2 = "O"
elif player1 == "O":
player2 == "X"
print ("The positions on the Tic Tac Toe will be similar to that of the Numpad. ")
Marker1 = input("Please enter a number between 1 - 9 to place your {} in the corresponding position ".format(player1))
print (player_input())
print ("\n")
display_board(testboard)
place_marker()