Discuss / Python / 交抄的作业

交抄的作业

Topic source

lostanother

#1 Created at ... [Delete] [Delete and Lock User]
def triangles():
    L = [1]
    n = 1
    while True:
      yield L
      L.append(0)
      L = [L[t-1]+L[t] for t in range(len(L))]
n = 0
for t in triangles():
    print(t)
    n = n + 1
    if n == 10:
        break

卡了一天,还没想到怎么用列表生成式的方法,最后加个0真是巧妙啊。 卡的地方是吧方法当成返回值用了,写了个L = L.insert(0,1)结果一直报错。


  • 1

Reply