【问题标题】:python: how can i make turtle perform certain actions from a list?python:我怎样才能让乌龟从列表中执行某些动作?
【发布时间】:2018-02-20 09:45:18
【问题描述】:
data_sets = [
    ['O', ['Sheet C', 'Location 2', 'Upright'],
          ['Sheet B', 'Location 3', 'Upright'],
          ['Sheet D', 'Location 1', 'Upright'],
          ['Sheet A', 'Location 4', 'Upright']]
    ['X', ['Sheet A', 'Location 1', 'Upright'],
          ['Sheet B', 'Location 2', 'Upright'],
          ['Sheet C', 'Location 3', 'Upright'],
          ['Sheet D', 'Location 4', 'Upright']],
]

我需要能够将正确的工作表粘贴到正确的位置。我当前的代码能够转到正确的位置,但它只能粘贴 sheet_a_upright() 而不是我想要的工作表,例如它适用于此列表,但随后会粘贴一张额外的工作表:

data_sets = [
    ['O', ['Sheet A', 'Location 1', 'Upright']]
]

代码可以粘贴到正确的位置,但不能粘贴到正确的工作表上。如果我让它粘贴我在问题开头放置的前 2 个列表之一,它只会在所有 4 个位置粘贴工作表 A。 我的代码如下:

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

#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()
            return True
        elif len(style)>1 and 'Sheet B' in style[1]:
            sheet_b_upright()
            return True
        elif len(style)>1 and 'Sheet C' in style[1]:
            sheet_c_upright()
            return True
        elif len(style)>1 and 'Sheet D' in style[1]:
            sheet_d_upright()
            return True

#define sheet outline and fill
def outline():
    pencolor('black')
    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():
    outline()
def sheet_b_upright():
    outline()
def sheet_c_upright():
    outline()
def sheet_c_upright():
    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)
        if sheet():
            return
#the number i put into data_sets[] depends on which list i want to paste
paste_up(data_sets[0])

我怎样才能让我的代码将正确的工作表粘贴到正确的位置,然后在结束时停止粘贴?(我没有包含工作表 A 绘图的代码,因为它太长而且不重要,大纲功能只是在位置周围绘制边框)

【问题讨论】:

    标签: python list turtle-graphics


    【解决方案1】:

    首先,您似乎打错了 - sheet_c_upright() 被定义了两次。

    其次,您所有的工作表函数都调用了 outline() - 这是故意的吗?如果不是,这可能就是为什么在所有位置都绘制同一张纸的原因。

    【讨论】:

    • 轮廓是故意的,我没有包含图纸的绘图代码,因为它只是一段很长的海龟指令。问题似乎与 goto_loc() 和/或 sheet() 定义有关,因为代码似乎只能粘贴 sheet_a_upright()。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-03
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多