如何让python先遍历for循环的特定部分?
假设我们有一个列表(这个列表中有更多的列表,但我只包含了一个用于演示的列表): 我做了一个这样的函数:如何让python先遍历for循环的特定部分?,python,for-loop,boolean,conditional,turtle-graphics,Python,For Loop,Boolean,Conditional,Turtle Graphics,假设我们有一个列表(这个列表中有更多的列表,但我只包含了一个用于演示的列表): 我做了一个这样的函数: def example(lists): for element in lists: if element[0] == 'House A': if element[1] == 'Location 4': if element[2] == 'Right': direction =
def example(lists):
for element in lists:
if element[0] == 'House A':
if element[1] == 'Location 4':
if element[2] == 'Right':
direction = 0
house1(loc4, direction)
elif element[2] == 'Left':
direction = 180
house1(loc4, direction)
if element[0] == 'X':
graffiti()
我怎样才能使它在for循环后都是真的(在本例中我可能没有正确地执行for循环,但在我的代码的正确版本中它是正确的),它会绘制涂鸦()。(做海龟图形)我没有测试这段代码:
direction_map = {'Right': 0, 'Left': 180}
def example(lists):
for element in lists:
if element[0] == 'House A' and element[1] == 'Location 4':
house1(loc4, direction_map[element[2]])
if list(element for element in lists if (element[0] == 'House A'
and element[1] == 'Location 4')) and (element[2] in direction_map)):
....
if element[0] == 'X':
graffiti()
但是从你发布的内容中我了解到,可能任何方法都是你想要的。编写代码块时,使用4个空格的缩进代替反勾号,反勾号仅用于内联代码还有一个“代码格式”按钮和实时预览…“但在我的代码的正确版本中它是正确的”。那你为什么不给我们展示一下正确的版本呢?你所说的“for循环后一切都是真实的”到底是什么意思?什么是“一切”?@Jarek.D没有为作业显示正确的版本,如果出现这种情况,也不想得到任何plaigarism。
direction_map = {'Right': 0, 'Left': 180}
def example(lists):
for element in lists:
if element[0] == 'House A' and element[1] == 'Location 4':
house1(loc4, direction_map[element[2]])
if list(element for element in lists if (element[0] == 'House A'
and element[1] == 'Location 4')) and (element[2] in direction_map)):
....
if element[0] == 'X':
graffiti()