Ok, so i have the loop that creates the shape below, but instead of spaces, it has solid # signs.
I can't figure out how to just get the design I showed below. Where am I going wrong in my code?

NUM_STEPS = 6
def main():
    for r in range(NUM_STEPS):
        for c in range (r):            
            print ('#',end='')
        print('#')
main()




##
# #
#  #
#   #
#    #
#     #

Recommended Answers

All 9 Replies

I think you are making it too complicated. Try this:

for r in range(NUM_STEPS):
    print("#" + r*" " + "#")
    print()

You are so correct. I got so many variations, but that is what i am looking for. However, the assignment calls for nested loop, so that's what I am trying to do.

I modified it, but now i get the pattern below.

# declare variable
NUM_STEPS = 6
#the program will print a pattern
def main():
    for r in range(NUM_STEPS):
      for c in range (r + 1):            
        print ('#',' '*c,'#')


main()


#  #
#  #
#   #
#  #
#   #
#    #
#  #
#   #
#    #
#     #
#  #
#   #
#    #
#     #
#      #
#  #
#   #
#    #
#     #
#      #
#       #

I'll play with it and see what I can figure out. Nested loops always play tricks with my head.

OK, in that case... actually, you're quite close to it, as it happens. You simply want to have another print line to print a hash before the inner loop, and change the print inside the loop to printing a space:

NUM_STEPS = 6
def main():
    for r in range(NUM_STEPS):
        print ('#',end='')
        for c in range (r):
            print (' ',end='')
        print('#')

main()

Schol, i tried that, but when i did it I got the right pattern, but the righ hand # was on the next line. Let me play with it.

David, Thanks again!

No Problem dusto.
I think I got it:

NUM_STEPS = 6
for r in range(NUM_STEPS):
    for c in range(r, r + 1):
        print("#" + c*" " + "#")

The way you wrote that looks simple. What gets me is that before i see it, it's challenging, then I see what you wrote there, and it all makes sense.

And schol, I didn't try that one earlier, apparently. That works too and I must have been close. Thanks.

Of course then you can make all at one line:

print '#' + '#\n#'.join(' ' * n for n in range(NUM_STEPS))+'#' if NUM_STEPS else ''
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.