[英]How do I minimize the space between characters in my nested loop?
so I'm trying to get my nested loop to display an image like in this 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.
与给定的图像相比,我得到的 output 似乎每行都有额外的空间,我不知道如何摆脱空间。 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.我认为这是 'end=' '' 声明,但如果我尝试用一个空格替换它,我的整个行都会变得不稳定。
Thanks!谢谢!
end=" "
prints an space instead of a newline in the end.. 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: output:
##
# #
# #
# #
# #
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("#")
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("#")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.