Print a triangular pattern of asterisks

后端 未结 15 961
慢半拍i
慢半拍i 2020-11-30 11:01

I am required to use nested for loops and print(\'*\', end=\' \') to create the pattern shown here:

And here is my code. I have figured out the first t

相关标签:
15条回答
  • 2020-11-30 11:11
    print("Pattern C")
    n=int(input("Enter number of rows: ")) 
    for i in range (n,0,-1):
        print((n-i) * ' ' + i * '*')
    
    0 讨论(0)
  • 2020-11-30 11:13

    Pythonic way of doing this. Just 1 line of code (using list comprehension) for each pattern.

    n = 10
    
    # for A
    print('\n'.join(['*'*i for i in range(1,n+1)]))
    
    #output
    *
    **
    ***
    ****
    *****
    ******
    *******
    ********
    *********
    **********
    
    
    # for B
    print('\n'.join(['*'*i for i in range(n+1,0,-1)]))
    
    #output
    ***********
    **********
    *********
    ********
    *******
    ******
    *****
    ****
    ***
    **
    *
    
    # for C
    print('\n'.join([' '*(n-i) + '*'*i for i in range(n,0,-1)]))
    
    #output
    **********
     *********
      ********
       *******
        ******
         *****
          ****
           ***
            **
             *
    
    
    # for D
    print('\n'.join([' '*(n-i) + '*'*i for i in range(1,n+1)]))
    
    #output
             *
            **
           ***
          ****
         *****
        ******
       *******
      ********
     *********
    **********
    
    0 讨论(0)
  • 2020-11-30 11:14

    using spaces you can create different- different patterns: 1.

    def pattern(limit):
    
        for i in range(limit+1):
            print((limit-i)*"  "+ ' #'*i)    
    
    pattern(4)
    
    Output: 
           #
         # #
       # # #
     # # # #
    

    2.remove one space from above code and pattern will be changed in a pyramid

    def pattern(limit):
        for i in range(limit+1):
            print((limit-i)*" "+ ' #'*i)       
    pattern(4)
    
    output: #
           # #
          # # #
         # # # # 
    

    3: no space print((limit-i)*""+ ' #'*i) will create

     #
     # #
     # # #
     # # # #
    
    0 讨论(0)
  • 2020-11-30 11:15
    i=0
    while(i<5):
        j=0
        while(j<=i):
            print("*",end="") #end="" is given to stay on same line
            j=j+1
        print("")  #it will take u to new line
        i=i+1
        j=0
    i=i-2
    j=i
    while(i>=0):
        while(j>=0):
            print("*",end="")
            j=j-1
        print() #will also work
        i=i-1
        j=i
    

    this will also work

    0 讨论(0)
  • 2020-11-30 11:16

    learn an easy way:
    code1:

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

    what it does:

    1. for n in range(0,5): --> call a range of no of times you want loop to execute
    2. increment value of int n by one, so starting from range 0 to 5 every time for loop runs it increments int n by 1
    3. now print string "" and multiply it by value of int n+0, So when int n=0 as per logic int n increments by 1 and print ("" (0+n)) = print ("" *(0+1)) = *

    output:
    *
    **
    ***
    ****
    *****

    code2:

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

    output:
    ****
    ***
    **
    *

    0 讨论(0)
  • 2020-11-30 11:19
    def fun (n):
        return n
    n=int(raw_input("Enter the number"))
    print "The given number is",n
    for i in range (n,0,-1):
        print "*"*i
    

    output:

    The given number is 3
    ***
    **
    *
    
    0 讨论(0)
提交回复
热议问题