定义一个函数,该函数将接受形状的长度列表和 return 每个点的坐标

Define a function that will take in a list of lengths from a shape and return the coordinates of each point

我正在尝试定义一个函数,该函数将从这样的形状中获取长度列表:

和return每个点的坐标。

这是到目前为止的内容:

def cord(lst):
    lst2 = [[0,0]]
    d = 'up'
    c = [0,0]
    for n in lst:
        if d == 'up': # if the direction is up, we add the value to the y cor
            c[1] += n
            lst2.append(c)
            d = 'right' # After up, we go right

        else: # if the direction is right, we add the value to the x cor
            c[0] += n
            lst2.append(c)
            d = 'up' # After right, we go up

    print(lst2)

cord([10,10,10])

输出:

[[0, 0], [10, 20], [10, 20], [10, 20]]

期望的输出:

[[0, 0], [0, 10], [10, 10], [10, 20]]

你能告诉我哪里出了问题吗?

由于list是可变的,而你的变量c是一个list,你需要附加一个c的副本,这样它就不会影响其他人你改变它。做:

lst2.append(c.copy())