[英]Removing Extra blank lines Python
Hi I was making a code that turns input into a way that it seemed as if you were talking like a snake(IDK I was bored) and in the while loop it seems to print extra blank lines, how can I change my code to remove those extra lines?. 嗨,我正在编写代码,将输入转换为某种方式,好像您在说蛇一样(IDK,我很无聊),而在while循环中,它似乎打印了多余的空白行,我该如何更改代码以删除那些多余的线? my code:
我的代码:
line = input("Line: ")
while line != "":
line = input("Line: ")
line = line.replace('s','sss')
line = line.replace('S','Sss')
print(line)
desired output(my output is that same except after 3rd 'Line: ' I have a blank line below(SO didn't allow my blank line as valid code)): 所需的输出(我的输出是相同的,除了第3行之后:我下面有一个空行(所以不允许我的空行作为有效代码)):
Line: Say, what sound does a snake make?
Sssay, what sssound doesss a sssnake make?
Line: Hiss
Hissssss
Line:
It looks like what you meant is this: 看起来您的意思是这样的:
line = input("Line: ")
while line:
line = line.replace('s','sss')
line = line.replace('S','Sss')
print(line)
line = input("Line: ")
So the print
is inside the loop, and it inputs a new line at the end of your loop, right before the while
condition checks if it is empty. 因此,
print
位于循环内部 ,并且在while
条件检查其是否为空之前,它将在循环结束时输入新行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.