2

The output I am trying to achieve is :

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

The code I have is :

NUM_STEPS = 6

for r in range(NUM_STEPS):
   for c in range(r):
      print(' ', end='')
   print('#','\t')
   print('#')   

Its close, but not quite the output I am trying to achieve. Any help or suggestions are most appreciated.

2
  • Look into the format function Commented Apr 20, 2014 at 15:45
  • 2
    If you love one-liners: print(*((' '*x).join('##') for x in range(6)), sep='\n') Commented Apr 20, 2014 at 15:57

2 Answers 2

4

The main thing is you should use '+' (or concat) to build up a string before printing it. You can eliminate the inner loop by using '*' to make r spaces, which cleans things up a lot.

NUM_STEPS = 6
for r in range(NUM_STEPS):
    print("#" + (' ' * r) + "#")
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, the * operator for strings really is handy :)
1

This seemed to work when I tried it:

for r in range(NUM_STEPS):
    print("#", end = "")
    for c in range(r):
        print(" ", end = "")
    print("#")

I hope it helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.