0

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()




##
# #
#  #
#   #
#    #
#     #
4
Contributors
9
Replies
38
Views
3 Years
Discussion Span
Last Post by pyTony
0

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

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

Edited by David1123

0

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.

Edited by dusto: nested loop required

0

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()


#  #
#  #
#   #
#  #
#   #
#    #
#  #
#   #
#    #
#     #
#  #
#   #
#    #
#     #
#      #
#  #
#   #
#    #
#     #
#      #
#       #
0

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

0

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()

Edited by Schol-R-LEA

0

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!

0

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*" " + "#")

Edited by David1123

0

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.

0

Of course then you can make all at one line:

print '#' + '#\n#'.join(' ' * n for n in range(NUM_STEPS))+'#' if NUM_STEPS else ''

Edited by pyTony

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.