Printing Simple Diamond Pattern in Python

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

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

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


        
相关标签:
15条回答
  • 2020-12-19 23:32

    simple way ...

    n= 11 #input is even number 1,3,5,...
    a = 1
    b = 1
    for a in range(n+1):
        if a%2 != 0:
            val = (n - a) // 2
            print (" "*val + "*"*(a) + " "*val ,end = "\n")
    for b in range(n-1,0,-1):
        if b%2 != 0:
            val2 = (n-b)//2
            print (" "*val2 + "*"*(b) + " "*val2 ,end = "\n")
    

    Output:

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

    Or using reverse method, first one is diamond, second one is series of diamond

    import copy
    n = 10       #input: size of diamon
    raw  = []
    lst = []
    re_lst = []
    
    for a in range(n+1):
        if a%2 != 0:
            val = (n - a) // 2
            raw = ''.join(" "*val + "*"*(a) + " "*val)
            lst.append(raw)
            re_lst = copy.deepcopy(lst)
    lst.reverse()
    #print diamond
    for i in re_lst:
        print(i)
    for i in lst:
        print(i)
    print("\n")
    #print series of diamond
    for i in re_lst:
        print(i)
        for i in lst:
            print(i)
    
    0 讨论(0)
  • 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))
    

    0 讨论(0)
  • 2020-12-19 23:34
    def pattern2(row):
        s=1
        c = row / 2
        d = int(c)-1
        for i in range(1,row+1):
            if i<c:
                print(" "*d,star(s))
                s+=2
                d-=1
            elif i==(c+0.5):
                print(star(row))
                s=s-2
                d=0
            else:
                print(" "*d,star(s))
                s=s-2
                d+=1
    def star(s):
        return '*'*s
    def main():
        row=int(input("enter the no. of row but the rows should be odd \n#special case of pattern"))
        try:
            a=row%2
            assert a!=0 and row!=0
            pattern2(row)
        except:
            print('Worng selection of rows for the perfect diamond.')
    if __name__=="__main__":
        main()
    
    0 讨论(0)
  • 2020-12-19 23:37

    Here is a solution base on height equals to top to the middle, or half of the height. For example, height is entered as 4(7) or 5(9) below. This method will yield odd number actual height

    h = eval(input("please enter diamond's height:"))
    
    for i in range(h):
        print(" "*(h-i), "*"*(i*2+1))
    for i in range(h-2, -1, -1):
        print(" "*(h-i), "*"*(i*2+1))
    
    # please enter diamond's height:4
    #      *
    #     ***
    #    *****
    #   *******
    #    *****
    #     ***
    #      *
    #
    # 3, 2, 1, 0, 1, 2, 3  space
    # 1, 3, 5, 7, 5, 3, 1  star
    
    # please enter diamond's height:5
    #       *
    #      ***
    #     *****
    #    *******
    #   *********
    #    *******
    #     *****
    #      ***
    #       *
    #
    # 4, 3, 2, 1, 0, 1, 2, 3, 4  space
    # 1, 3, 5, 7, 9, 7, 5, 3, 1  star
    

    Here is another solution base on height equals to top to the bottom, or the actual total height. For example, height is entered as 7 or 9 below. When the user enters an even number for height, the diamond will be slightly slanted.

    h = eval(input("please enter diamond's height:"))
    
    for i in range(1, h, 2):
        print(" "*(h//2-i//2), "*"*i)
    for i in range(h, 0, -2):
        print(" "*(h//2-i//2), "*"*i)
    
    # please enter diamond's height:7
    #      *
    #     ***
    #    *****
    #   *******
    #    *****
    #     ***
    #      *
    #
    # 3, 2, 1, 0, 1, 2, 3  space
    # 1, 3, 5, 7, 5, 3, 1  star
    #
    # please enter diamond's height:9
    #       *
    #      ***
    #     *****
    #    *******
    #   *********
    #    *******
    #     *****
    #      ***
    #       *
    #
    # 4, 3, 2, 1, 0, 1, 2, 3, 4  space
    # 1, 3, 5, 7, 9, 7, 5, 3, 1  star
    
    0 讨论(0)
  • 2020-12-19 23:37
    #maybe it could help  
    
    height = eval ( input ( 'How high? ' ) )
    height = int (height//2)
    
    for i in range(1, height+1):
        print(' ' *(height-i+1), '*'*i + '*' * (i-1) )
    
    for i in range (height+1, 0, -1):
        print (' ' * (height+1-i), '*' * i + '*' * (i-1))  
    
    0 讨论(0)
  • 2020-12-19 23:38

    Simplest Answer

    n=5
    for i in range(1,n+1):
        print ((n-i)*(" ")+(i*" *"))
    
    for i in range(n-1,0,-1):
        print((n-i)*(" ")+(i*" *"))
    

    Hope this helps some one

    0 讨论(0)
提交回复
热议问题