Printing an ASCII diamond with set width in python

前端 未结 7 1664
Happy的楠姐
Happy的楠姐 2020-12-11 13:50

Yes, this is a homework task. But just please, if you\'re going to give me the code please tell me what you\'ve done in detail. I am extremely new to this.

So the ta

相关标签:
7条回答
  • 2020-12-11 14:36

    One method

    The simplest way would probably be have two loops; one counting i up to width, another counting i back down to 1.

    width = int(input("Width: "))
    
    i = 1
    while i < width:
        print " " * (width-i) + "* " * i
        i += 1
    
    while i > 0:    
        print " " * (width-i) + "* " * i
        i -= 1
    

    This is a bit unattractive because it's a little clumsy, but it's simple.


    Another method

    Another method is to have have a loop that counts to twice the width, doing one of two things. What it does depends on if i has passed the point of maximum width or not. So it does 'up' and 'down' in the same loop, counting i from 1 up to width*2.

    width = int(input("Width: "))
    
    i = 1
    while i < width*2:
        if i < width:
            print " " * (width-i) + "* " * i
        else:
            print " " * (i-width) + "* " * (2*width-i)
    
        i += 1
    

    This:

     print " " * (width-i) + "* " * i
    

    ...is your code. Spaces count from width down to 0, *'s from 1 up to width.

    And this:

     print " " * (i-width) + "* " * (2*width-i)
    

    ...is the same thing but inverted. Spaces count from 0 back up to width, and the *'s go back down from width to 1. This comes into play when i exceeds width.

    Width: 4
       *      # first half does this onward
      * * 
     * * * 
    * * * * 
     * * *    # second half does the rest downward
      * * 
       * 
    

    And another

    Another alternative, more complex way is to use a for loop on a list that contains numbers counting up and down. For example: [1, 2, 3, 2, 1]

    To make this list, this code has to be. I know, it's a bit ugly:

    rows = []
    for i in range(1, max+1):
        rows.append(i)
    rows += rows[-2::-1]
    

    Then, you see, we run the for loop off it.

    width = int(input("Width: "))
    
    rows = []
    for i in range(1, width+1):
        rows.append(i)
    rows += rows[-2::-1]    # takes a reversed list and adds it on to the end: [1, 2, 3, 2, 1]
    
    for i in rows:
        print " " * (width-i) + "* " * i
    

    i iterates through each of the numbers in the rows list, which looks something like [1, 2, 3, 2, 1]. Then we just need one printing gizmo.

    In python, there's almost always a shorter and less comprehensible way of doing for loops, and in this case, we can get rid of two extra lines by shortening the first for loop:

    width = int(input("Width: "))
    
    rows = [ i for i in range(1, width+1)]  # Brain-bending way of doing a for loop 
    rows += rows[-2::-1]
    
    for i in rows:
        print " " * (width-i) + "* " * i
    

    And if you're feeling a bit crazy, here's a mere two line version of the whole thing!

    width = int(input("Width: "))
    print "\n".join([ " "*(width-i) + "* "*i for i in [ i for i in range(1, width+1) ]+[ i for i in range(1, width+1) ][-2::-1] ])
    

    But I don't recommend this style of coding in general.


    Sorry, I got a bit carried away at the end... but the best thing I can say to you now is try everything and play around!

    Hope that helps. :)

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