【发布时间】:2020-04-15 05:26:30
【问题描述】:
在这个游戏中,'o'表示的位置(0, 0)只有一个元素。其余的都被占用了。编写一个函数empty(game),它接受一个游戏并给出所有空格的位置列表。每个位置由(i,j) 指定。
例如,
game = [['o', 'x', 'x'],
['x', 'x', 'x'],
['x', 'x', 'x']]
empty(game) 给出 [(0,0)]
def empty_spaces(game):
result = [0]
num_of_rows = len(game)
num_of_columns = len(game[0])
for i in range(num_of_columns):
for j in range(num_of_rows):
if not "o":
result += i[j]
return result
但是,我得到的结果是[0]。非常感谢您的帮助!
【问题讨论】:
-
提示:仔细查看
if not 'o':的值,或者更具体地说,查看not 'o'的值。 -
if not "'o" 是否更改为 if "o" ?
-
不。
if "o"无论如何都会是真的。 -
您可以检查
board[j][i]中的值,稍后您必须添加(i,j)。并在星创建空列表 -result = []。顺便说一句:请记住,您使用坐标[row][column],而不是[column][row]
标签: python python-3.x list search