def yhui(n): a,b,c=0,[1],[1] while a<n: yield b b.append(0) c=b b=[v+w for v,w in zip(c,reversed(c))] a=a+1 return '结束'#杨辉三角的前n行 def yanghui(n): g=yhui(n) while True: try: x=next(g) print(x) except StopIteration as e: print('生成器返回值:',e.value) break#生成并打印杨辉三角的前n行
刚开始学python,三句代码不知道怎么合一句,总是报错。。。只好舍近求远,不过总算表达出意思了。
Sign in to make a reply
萧江林