python:我怎样才能让我的功能一旦完成我想要的就停止阅读列表

时间:2017-09-12 01:20:31

标签: python list function

我需要能够使用列表中的数据将4张纸粘贴到广告牌背景上,这里是该列表的一小部分:

data_sets = [
# These two initial data sets don't put any sheets on the billboard
# Data sets 0 - 1
['O'],
['X'],
# These data sets put Sheet A in all possible locations and orientations
# Data sets 2 - 9
['O', ['Sheet A', 'Location 1', 'Upright']],
['O', ['Sheet A', 'Location 2', 'Upright']],
['O', ['Sheet A', 'Location 3', 'Upright']],
['O', ['Sheet A', 'Location 4', 'Upright']],
['O', ['Sheet A', 'Location 1', 'Upside down']],
['O', ['Sheet A', 'Location 2', 'Upside down']],
['O', ['Sheet A', 'Location 3', 'Upside down']],
['O', ['Sheet A', 'Location 4', 'Upside down']]
]

我正试图让乌龟画出我的画面,但是一旦画出它,它就会继续浏览整个列表并绘制轮廓,我需要它一旦执行了sheet_a_upright()就停止浏览列表。 'x'和'o'在这一点上没有任何意义,也没有注意到它们。同样的事情发生在我的goto_loc()函数中,但我通过将data_sets作为参数来修复它,当我为sheet()函数执行此操作时,它最终根本没有绘制任何东西。

#location function for data_sets
def goto_loc(data_sets):
    for location in data_sets:
        if len(location)>1 and 'Location 1' in location[1]:
            goto(-300, 0)
        elif len(location)>1 and 'Location 2' in location[1]:
            goto(-100, 0)
        elif len(location)>1 and 'Location 3' in location[1]:
            goto(100, 0)
        elif len(location)>1 and 'Location 4' in location[1]:
            goto(300, 0)

#function for which sheet should be drawn from data_sets
def sheet():
    for style in data_sets:
        if len(style)>1 and 'Sheet A' in style[1]:
            sheet_a_upright()
        elif len(style)>1 and 'Sheet B' in style[1]:
            sheet_b_upright()
        elif len(style)>1 and 'Sheet C' in style[1]:
            sheet_c_upright()
        elif len(style)>1 and 'Sheet D' in style[1]:
            sheet_d_upright()

#define sheet outline and fill
def outline():
    penup()
    forward(100)
    pendown()
    fillcolor('green')
    begin_fill()
    left(90)
    fd(250)
    left(90)
    fd(200)
    left(90)
    fd(500)
    left(90)
    fd(200)
    left(90)
    fd(250)
    right(90)
    penup()
    end_fill()

#function for sheet A in upright position
def sheet_a_upright():
    #sheet outline and fill
    outline()

# Paste the sheets onto the billboard as per the provided data set
def paste_up(data_sets):
    for each in data_sets:
        goto_loc(data_sets)
        sheet()

paste_up(data_sets[2])

1 个答案:

答案 0 :(得分:2)

如果执行了sheet,请使用return True函数sheet_a_upright()

def sheet():
            for style in data_sets:
                if len(style)>1 and 'Sheet A' in style[1]:
                    sheet_a_upright()
                    return True
                elif len(style)>1 and 'Sheet B' in style[1]:
                    sheet_b_upright()
                elif len(style)>1 and 'Sheet C' in style[1]:
                    sheet_c_upright()
                elif len(style)>1 and 'Sheet D' in style[1]:
                    sheet_d_upright()

然后,在paste_up函数中,检查sheet()是否为真:

def paste_up(data_sets):
            for each in data_sets:
                goto_loc(data_sets)
                if sheet():
                    return