如何使用嵌套while循环使这个数字三角形? (python 2.7)

时间:2016-10-01 14:57:27

标签: python-2.7

我试图使用下面给出的代码制作这种模式....我知道它需要进一步改变,但无法弄清楚是什么?

Number Triangle

 n=5
 m=1
 while n>=1:
     while m<=5:
       print " "*(n),m
       n=n-1
       m=m+1

我想要这样的输出:

    1
   22
  333
 4444
55555

1 个答案:

答案 0 :(得分:0)

您需要嵌套循环吗?

>>> n=5
>>> for i in range(1, n+1):
...     print("{:>{width}}".format(str(i)*i, width=n))
    1
   22
  333
 4444
55555

但是为了修复你的代码 - 你错过了数字的乘数:

n=5
m=1
while n>=1:
    while m<=5:
        print " "*(n), str(m)*m
        n=n-1
        m=m+1