'break' 关键字是否阻止代码 运行 在 Python 中再次循环?
Does the 'break' keyword prevent the code from running that loop again in Python?
这是我的代码,其中我尝试在创建元组映射列表时引用元组的墙列表,它的索引 [variable][2] 作为 属性墙壁或地板:
map = []
walls = [(0,3), (0,4), (0,5), (0,7), (2,7), (3,7), (4,7), (5,7), (1,7),
(5,6), (5,4), (2,3), (2,4), (3,5), (3,0), (3,1), (3,2), (7,0), (7,1), (7,5),
(7,6), (7,7), (8,7)]
for x in range(9):
for y in range(9):
for a in range(len(walls)):
if walls[a][0] == x and walls[a][1] == y:
map.append(tuple((x,y,"w")))
else:
map.append(tuple((x,y,"_")))
break
---> [(0, 0, '_'), (0, 1, '_'), (0, 2, '_'), (0, 3, 'w'), (0, 4, '_'), (0, 5, '_'), (0, 6, '_'), (0, 7, '_'), (0, 8, '_'), (1, 0, '_'), (1, 1, '_'), (1, 2, '_'), (1, 3, '_'), (1, 4, '_'), (1, 5, '_'), (1, 6, '_'), (1, 7, '_'), (1, 8, '_'), (2, 0, '_'), (2, 1, '_'), (2, 2, '_'), (2, 3, '_'), (2, 4, '_'), (2, 5, '_'), (2, 6, '_'), (2, 7, '_'), (2, 8, '_'), (3, 0, '_'), (3, 1, '_'), (3, 2, '_'), (3, 3, '_'), (3, 4, '_'), (3, 5, '_'), (3, 6, '_'), (3, 7, '_'), (3, 8, '_'), (4, 0, '_'), (4, 1, '_'), (4, 2, '_'), (4, 3, '_'), (4, 4, '_'), (4, 5, '_'), (4, 6, '_'), (4, 7, '_'), (4, 8, '_'), (5, 0, '_'), (5, 1, '_'), (5, 2, '_'), (5, 3, '_'), (5, 4, '_'), (5, 5, '_'), (5, 6, '_'), (5, 7, '_'), (5, 8, '_'), (6, 0, '_'), (6, 1, '_'), (6, 2, '_'), (6, 3, '_'), (6, 4, '_'), (6, 5, '_'), (6, 6, '_'), (6, 7, '_'), (6, 8, '_'), (7, 0, '_'), (7, 1, '_'), (7, 2, '_'), (7, 3, '_'), (7, 4, '_'), (7, 5, '_'), (7, 6, '_'), (7, 7, '_'), (7, 8, '_'), (8, 0, '_'), (8, 1, '_'), (8, 2, '_'), (8, 3, '_'), (8, 4, '_'), (8, 5, '_'), (8, 6, '_'), (8, 7, '_'), (8, 8, '_')]
问题:只有一个坐标被检查并分配'w',而其余的都没有。我看到问题的逻辑点是 break 语句。 break 语句是否再次阻止 运行 中的最后一个嵌套 for 循环。它不应该:x=0, y=3, a=0, break ---> x=0, y=4, a=0, a=1, break --->...
来自 python 文档:
The break statement, like in C, breaks out of the smallest enclosing for or while loop.
进一步阅读 here。
Yahli,希望这对你有所帮助。
这是我的代码,其中我尝试在创建元组映射列表时引用元组的墙列表,它的索引 [variable][2] 作为 属性墙壁或地板:
map = []
walls = [(0,3), (0,4), (0,5), (0,7), (2,7), (3,7), (4,7), (5,7), (1,7),
(5,6), (5,4), (2,3), (2,4), (3,5), (3,0), (3,1), (3,2), (7,0), (7,1), (7,5),
(7,6), (7,7), (8,7)]
for x in range(9):
for y in range(9):
for a in range(len(walls)):
if walls[a][0] == x and walls[a][1] == y:
map.append(tuple((x,y,"w")))
else:
map.append(tuple((x,y,"_")))
break
---> [(0, 0, '_'), (0, 1, '_'), (0, 2, '_'), (0, 3, 'w'), (0, 4, '_'), (0, 5, '_'), (0, 6, '_'), (0, 7, '_'), (0, 8, '_'), (1, 0, '_'), (1, 1, '_'), (1, 2, '_'), (1, 3, '_'), (1, 4, '_'), (1, 5, '_'), (1, 6, '_'), (1, 7, '_'), (1, 8, '_'), (2, 0, '_'), (2, 1, '_'), (2, 2, '_'), (2, 3, '_'), (2, 4, '_'), (2, 5, '_'), (2, 6, '_'), (2, 7, '_'), (2, 8, '_'), (3, 0, '_'), (3, 1, '_'), (3, 2, '_'), (3, 3, '_'), (3, 4, '_'), (3, 5, '_'), (3, 6, '_'), (3, 7, '_'), (3, 8, '_'), (4, 0, '_'), (4, 1, '_'), (4, 2, '_'), (4, 3, '_'), (4, 4, '_'), (4, 5, '_'), (4, 6, '_'), (4, 7, '_'), (4, 8, '_'), (5, 0, '_'), (5, 1, '_'), (5, 2, '_'), (5, 3, '_'), (5, 4, '_'), (5, 5, '_'), (5, 6, '_'), (5, 7, '_'), (5, 8, '_'), (6, 0, '_'), (6, 1, '_'), (6, 2, '_'), (6, 3, '_'), (6, 4, '_'), (6, 5, '_'), (6, 6, '_'), (6, 7, '_'), (6, 8, '_'), (7, 0, '_'), (7, 1, '_'), (7, 2, '_'), (7, 3, '_'), (7, 4, '_'), (7, 5, '_'), (7, 6, '_'), (7, 7, '_'), (7, 8, '_'), (8, 0, '_'), (8, 1, '_'), (8, 2, '_'), (8, 3, '_'), (8, 4, '_'), (8, 5, '_'), (8, 6, '_'), (8, 7, '_'), (8, 8, '_')]
问题:只有一个坐标被检查并分配'w',而其余的都没有。我看到问题的逻辑点是 break 语句。 break 语句是否再次阻止 运行 中的最后一个嵌套 for 循环。它不应该:x=0, y=3, a=0, break ---> x=0, y=4, a=0, a=1, break --->...
来自 python 文档:
The break statement, like in C, breaks out of the smallest enclosing for or while loop.
进一步阅读 here。
Yahli,希望这对你有所帮助。