【发布时间】:2018-11-21 21:55:14
【问题描述】:
我正在创建一个函数来显示变量board。函数 display_board 的 else 部分应该将 board 的元素更改为“-”。第一个 if 语句用于我的程序的另一个变量,称为位置,它工作正常。 当我调用 display_board 时,它会输出正确的格式,但实际上并没有改变 board,正如它在打印 board 时所看到的那样。 任何想法为什么它不起作用?
旁注:这是针对 Python 中的介绍性编程课程,因此,嵌套列表与我的知识/课程范围一样先进。
board = [
[' ', ' ', ' '],
[' ', ' ', ' '],
[' ', ' ', ' ']
]
def display_board(board):
if board == locations:
for row in locations:
for column in row:
print(column, end=' ')
print()
# This chunk below is the important code that is not altering *board*
else:
for row in board:
for column in row:
if column == 'X':
print(column, end=' ')
elif column == 'O':
print(column, end=' ')
else:
column = '-'
print(column, end=' ')
print()
display_board(board)
print(board)
输出:
- - -
- - -
- - -
[['','',''],['','',''],['','','']]]
【问题讨论】:
标签: python function global-variables tic-tac-toe