0

so I'm trying to get my nested loop to display an image like in this picture:

Picture

So far, this is the code that I have.

for a in range (1):
    print("#""#")
    for b in range (0,5,1):
        print("#", end=" ")
        for c in range(b):
            print(" ", end=" ")
        print("#")

I'm new to the site, so please excuse my terrible formatting. The output I'm getting seems to have an extra space per line compared to the image given, and I'm not sure how to get rid of the space. I'd appreciate any help!

I'm thinking it's the 'end=' '' statement, but if I try replacing that with just a space, my entire line goes wonky.

Thanks!

3
  • What does end=“ “ do? Commented Oct 6, 2019 at 18:36
  • @barny it stops a newline between elements, so they get printed on the same line. Note that in both instances that you've asked this, you've got some fancy quotes which will do nothing but throw an error. I guess they come from an Apple device, though
    – roganjosh
    Commented Oct 6, 2019 at 18:40
  • Yeah I can’t help ios doing smart quotes, but happily you were smart enough to spot them. Perhaps you were too distracted by those quotes, but you are wrong or not entirely right: making end a space puts a space at the end of what is printed: surely it makes more sense to print a space and then make end ’’, i.e. nothing, which will suppress the newline. Commented Oct 7, 2019 at 6:50

3 Answers 3

1

end=" " prints an space instead of a newline in the end..

I think its better to concatenate the string in this case instead of manipulating the print's end..

for i in range(5):
    print('#' + ' '*i + '#')

output:

##
# #
#  #
#   #
#    #
3
  • thanks for the alternate code, unfortunately my question wanted me to use a forloop only..
    – aislinx
    Commented Oct 6, 2019 at 18:49
  • but could you please further explain how exactly does the ' '*i work? thanks!
    – aislinx
    Commented Oct 6, 2019 at 18:50
  • multiplying a sequence with a value(say n) results in the sequence concatenated with itself n times. Here str can be seen as a sequence of immutable...
    – Sam Daniel
    Commented Oct 7, 2019 at 7:15
0

You need to remove the whitespace in the 2nd end variable

for a in range (1):
print("#""#")
for b in range (0,5,1):
    print("#", end=" ")
    for c in range(b):
        print(" ", end="") #this end variable is what is causing your additional space
    print("#")
-1

Like this? Changed the third print

for a in range (1):
    print("#""#")
    for b in range (0,5,1):
        print("#", end=" ")
        for c in range(b):
            print(end=" ")
        print("#")
3
  • What does end=“ “ do? Why would you prefer to use that to print a space over actually print a space? Commented Oct 6, 2019 at 18:38
  • thank you! seems that my issue was just me arbitrarily adding an extra " " in the third print...
    – aislinx
    Commented Oct 6, 2019 at 18:44
  • 1
    This solution misuses the end kwarg for actual printing, a proper solution (if you don't want to concatenate a string prior to printing) would be to print an actual space and overwrite the end-newline with an empty string.
    – jbndlr
    Commented Oct 6, 2019 at 21:11

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.