Printing an ASCII diamond with set width in python

前端 未结 7 1663
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:16

    after your while

    i=i-2
    while i>0:
      print(" "*(wid-i)+"* "*i)
      i=i-1;
    
    0 讨论(0)
  • 2020-12-11 14:20

    You start with i = 1 and go until i > wid to make the top. To make the bottom of the diamond, you must do the reverse of what you did for the top. The code is easy, but I won't write it unless you want me to.

    0 讨论(0)
  • 2020-12-11 14:20

    check it out (for python 2.7x) :


    Filled ASCII Diamond :

    width = 1
    width += int(raw_input('Width : '))
    for i in range (1,width):
        for j in range (width,i,-1):
            print " ",
        for j in range (1,i,1):
            print " * ",
        print
    
    for i in range (width,1,-1):
        for j in range (width,i,-1):
            print " ",
        for j in range (1,i,1):
            print " * ",
        print
    

    This works!! But not in any Browser window . . .

    0 讨论(0)
  • 2020-12-11 14:23
     i=1
     j=input("ENTER NO =")
     l=0
     for i in range(i,j-((j/2)-1),1):
         print (' ' * ((j+1)/2-i)+'*' *(i*2-1))
         l=(j/2+1)
         while  (i==l):
            i=1
            for i in range(i,j,1):
                print (' ' *((i*2)-i)+'*' *(j-i*2))
            if [i==j-1]:
                l=raw_input('<press enter to exit>')       
    
    0 讨论(0)
  • 2020-12-11 14:29

    I tried to explain the code with comments. I hope it helps.

    wid = int(input("Width: "))
    
    #no. of lines will be double the width
    #each loop prints a line.
    for i in range(wid *2):
    
        #first half of the diamond
        if i<=wid:
            no_of_spaces = wid - i
            no_of_stars = i
            print(" "*no_of_spaces +  "* "*no_of_stars) 
    
        #next half of the diamond
        else:
            no_of_spaces = i - wid
            no_of_stars = 2*wid - i
            print(" "*no_of_spaces +  "* "*no_of_stars) 
    
    0 讨论(0)
  • 2020-12-11 14:35

    Since some good methods have been addressed, here are some fun little hacky solutions.

    Here's one using Python 2.7 string.center just for shits.

    import string    
    
    width = int(raw_input("Width:"))
    
    for i in range(width):
        print string.center(i * " *", width * 2 )
    
    for i in range(width,0,-1):
        print string.center(i * " *", width * 2 )
    

    And here's an outrageous one that ouputs using HTML to center.

    file = open('file.html','w')
    file.write("<div align='center'>")
    
    for i in range(width):
        file.write(i * " *") 
        file.write("<br>")
    
    for i in range(width,0,-1):
        file.write(i * " *")
        file.write("<br>")
    
    file.write("</div>")
    file.close() 
    
    import webbrowser
    webbrowser.open("file.html")
    
    0 讨论(0)
提交回复
热议问题