theBoard = {'top left': ' ', 'top middle': ' ', 'top right': ' ',
'center left': ' ', 'center middle': ' ', 'center right': ' ',
'bottom left': ' ', 'bottom middle': ' ', 'bottom right': ' '}
def printBoard(board):
print(board['top left'] + '|' + board['top middle'] + '|' + board['top right'])
print('-+-+-')
print(board['center left'] + '|' + board['center middle'] + '|' + board['center left'])
print('-+-+-')
print(board['bottom left'] + '|' + board['bottom middle'] + '|' + board['bottom right'])
turn = 'X'
for i in range (9):
printBoard(theBoard)
print('Turn for ' + turn + '.Move on which space?')
move = input()
theBoard[move] = turn
if turn == 'X':
turn = 'O'
else:
turn = 'X'
printBoard(theBoard)
答案 0 :(得分:0)
我认为您的缩进错了:
theBoard = {'top left': ' ', 'top middle': ' ', 'top right': ' ',
'center left': ' ', 'center middle': ' ', 'center right': ' ',
'bottom left': ' ', 'bottom middle': ' ', 'bottom right': ' '}
def printBoard(board):
print(board['top left'] + '|' + board['top middle'] + '|' + board['top right'])
print('-+-+-')
print(board['center left'] + '|' + board['center middle'] + '|' + board['center left'])
print('-+-+-')
print(board['bottom left'] + '|' + board['bottom middle'] + '|' + board['bottom right'])
turn = 'X'
for i in range (9):
printBoard(theBoard)
print('Turn for ' + turn + '.Move on which space?')
move = input()
theBoard[move] = turn
if turn == 'X':
turn = 'O'
else:
turn = 'X'
printBoard(theBoard)