无法在 python 中定义用户函数



无法在python 中定义任何用户定义的函数

缩进和拼写都是正确的,但我不知道到底发生了什么

错误图像

参考代码——


def display_board(board):

print( board[7],"|", board[8],"|", board[9] )
print("--|---|--")
print( board[4],"|", board[5],"|", board[6] )
print("--|---|--")
print( board[1],"|", board[2], "|",board[3] )
#********************************************************#
#test_board = ['#','X','O','X','O','X','O','X','O','X']
#display_board(xyz)
#********************************************************#
def player_input():

while True:
player_1 = input("Please select character ('X' or 'O'): ").upper()
if player_1 == "X" or player_1 == "O" :
break
elif player_1 != "X" or player_1 != "O" :
print("Wrong character!!!", end=" ") 

if player_1 == "X":
return print("X", "O")
else:
return print("O", "X")
#************************************************************************************#    
#player_input()
#************************************************************************************#
def place_marker(board, player_1, position):
board[position] = player_1

place_marker(test_board,'$',8)
display_board(test_board)
PS C:Userslohar> & C:/Users/lohar/AppData/Local/Programs/Python/Python310/python.exe "d:/Vs code/Programs/Tic_Tac_Toe.py"
Traceback (most recent call last):
File "d:Vs codeProgramsTic_Tac_Toe.py", line 14, in <module>
display_board(xyz)
NameError: name 'xyz' is not defined
PS C:Userslohar> & C:/Users/lohar/AppData/Local/Programs/Python/Python310/python.exe "d:/Vs code/Programs/Tic_Tac_Toe.py"
Please select character ('X' or 'O'): o
O X
Traceback (most recent call last):
File "d:Vs codeProgramsTic_Tac_Toe.py", line 40, in <module>
place_marker(test_boar,'$',8)
NameError: name 'test_boar' is not defined
PS C:Userslohar>

这样做,您将获得输出

def display_board(board):

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

test_board = ['#','X','O','X','O','X','O','X','O','X']  
display_board(test_board)

注意:只通过调用的。EX.这里定义了test_board,所以你必须通过test_board

相关内容

  • 没有找到相关文章