Printing Simple Diamond Pattern in Python

后端 未结 15 2577
执笔经年
执笔经年 2020-12-19 23:14

I would like to print the following pattern in Python 3.5 (I\'m new to coding):

    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *
         


        
15条回答
  •  -上瘾入骨i
    2020-12-19 23:33

    There are two version of this

    1. Space between Stars
    2. Without Space between Stars

    Space between Stars

    n = 4
    for i in range(n):
        print(' '*(n-i-1) + '* '*(i+1) )
    for i in range(n):
        print(' '*(i+1) + '* '*(n-i-1))
    

    Without Space between Stars

    n = 4
    for i in range(n):
        print(' '*(n-i-1) + '*'*((2*i)+1) )
    for i in range(n):
        print(' '*(i+1) + '*'*((2*((n-1)-i))-1))
    

提交回复
热议问题