Py学习  »  Python

在python中打印N个数字的菱形[闭合]

maya • 3 年前 • 331 次点击  

n = int(input("Enter Diamond Number Pattern Rows = "))

for item in range(n): # for loop
    p = 1 
    for j in range(item, n):
        print('',end=' ')
    for j in range(item+1):
        print(p, end=' ')
        p +=1 
    print()

enter image description here

我需要的输出。

   1  
  1 2  
 1 2 3  
  1 2    
   1 

但是我得到的输出。

   1  
  1 2  
 1 2 3  
  1 2    
    1

为什么?解释一下。在python中打印N个数字的菱形。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/136901