我正在用tic tac toe游戏在python中做一个小项目,但我遇到的问题是我的代码,尽管它不完整 现在我的问题是在运行代码时,我收到错误“位置”未定义 请解决我的问题,并预先感谢!
def display_board(testboard):
print(' | | ')
print(' '+testboard[1]+' | '+testboard[2]+' | '+testboard[3])
print(' | | ')
print('------------')
print(' | | ')
print(' ' + testboard[4] + ' | ' + testboard[5] + ' | ' +
testboard[6])
print(' | | ')
print('------------')
print(' | | ')
print(' ' + testboard[7] + ' | ' + testboard[8] + ' | ' +
testboard[9])
print(' | | ')
def board_marker():
marker = ' '
while not (marker == 'X' or marker =='O' or marker == 'x' or marker
=='o'):
marker = input('do you want x r o')
if marker.upper() == 'X' :
return ('X','O')
else:
return ('O','X')
def place_marker(board, position, marker):
board[positon] = marker
test_board= ['#','X','O','X','O','X','O','X','O','X','O']
place_marker(test_board, , '$')
display_board(test_board)
答案 0 :(得分:0)
尝试一下:
def display_board(testboard):
print(' | | ')
print(' '+testboard[1]+' | '+testboard[2]+' | '+testboard[3])
print(' | | ')
print('------------')
print(' | | ')
print(' ' + testboard[4] + ' | ' + testboard[5] + ' | ' +
testboard[6])
print(' | | ')
print('------------')
print(' | | ')
print(' ' + testboard[7] + ' | ' + testboard[8] + ' | ' +
testboard[9])
print(' | | ')
def board_marker():
marker = ' '
while not (marker == 'X' or marker =='O' or marker == 'x' or marker =='o'):
marker = input('do you want x r o')
if marker.upper() == 'X' :
return ('X','O')
else:
return ('O','X')
def place_marker(board, position, marker):
board[position] = marker # make it position here from positon as Nicco Haase said
test_board= ['#','X','O','X','O','X','O','X','O','X','O']
place_marker(test_board, 1, '$') # pass a value for position too don't keep it empty
display_board(test_board)
希望这会有所帮助... 谢谢