2016-11-06 97 views
0

我需要使用嵌套循环来产生三角形。输出需要看起来像这样。嵌套循环产生三角形,错误的方式

How many lines? 7 
0000000 
000000 
    00000 
    0000 
    000 
    00 
     0 

目前我试图使用这个,但我不确定如何让0面对正确的方式。

def main(): 
start = int(input("How many lines?")) 
end = 0 
increment = -1 

for rows in range(start,end,increment): 
    for colums in range(rows): 
     print("0", end= "") 
    print() 
main() 

而这个的输出是。

How many lines?7 
0000000 
000000 
00000 
0000 
000 
00 
0 

我只是不确定如何解决它,任何帮助表示赞赏。

+0

的可能的复制[Python的:打印星号的三角形图案(http://stackoverflow.com/questions/26352412/python-print-a-triangular-pattern-of-asterisks) – trincot

回答

0
a = int(input("how many lines?")) 
for e in range (a,0,-1): 
    print((11-e) * ' ' + e * '0') 
+1

请编辑您的发表一些关于你的代码的评论 - 它是如何工作的,注意事项是什么等等。 –