[Solved] Print Hexagonal pattern using asterisk [closed]


To work on arbitrary lengths>1, you have to change the second if-statement. I fixed the last one for you:

sideLength = 5
totalLength = (sideLength) + 2*(sideLength-1)

loop = 1

while loop<=totalLength :
    if loop==1 or loop==totalLength:
        print " "*(sideLength-1) + "*"* sideLength + " "*(sideLength-1)

    if loop>sideLength-1 and loop<= 2*sideLength-1:
        print "*" + " "*(totalLength-2) + "*"

    if loop>=1 and loop<sideLength-1:
        print " "*(sideLength- 1- loop) + "*" +  " " * ((sideLength-2) + 2*(loop)) + "*" + " "*(sideLength- 1- loop)

    if loop>=2*sideLength-1 and loop<totalLength-1:
        print " "*(loop-totalLength+sideLength) + "*"+  " " *(totalLength-2*(loop-totalLength+sideLength+1)) + "*" + " "*(sideLength- 1- loop)

    loop+=1

1

solved Print Hexagonal pattern using asterisk [closed]