英文:
python: Cant figure out how to make enter not submit input()
问题
我想能够在Python控制台中编写段落,然后使用类似SHIFT + ENTER的方式提交,但找不到方法来实现。
我的代码:
text = str(input("输入完整的文本: "))
print(text)
英文:
I wanted to be able to write paragraphs in the python console and then submit with something like SHIFT + ENTER but cant find a way to do it.
My code:
text = str(input("Enter the full text: "))
print(text)
答案1
得分: 0
我觉得你的问题很有趣,因为命令行不允许你按回车并继续输入段落,但我们可以通过使用一个while循环来移动到下一行并将新行附加到上一行来欺骗它以输入段落,只有当输入一个命令时才会停止捕获段落。
def get_input():
lines = []
print("输入你的段落(在新行上键入'/done'以完成输入):")
while True:
line = input()
if line == "/done":
break
lines.append(line)
return "\n".join(lines)
def main():
input_text = get_input()
print("输入的文本:")
print(input_text)
if __name__ == "__main__":
main()
你只需要在新行上输入/done
来退出循环并保存你的段落。
英文:
I found your question quite interesting because the command line does not allow you to hit enter and continue with your paragraph but we can trick it into being able to input paragraphs by using a while loop to move to the next line and append the new line to the previous line and will only stop capturing the paragraph only when a command is entered.
def get_input():
lines = []
print("Enter your paragraph (type '/done' on a new line to finish):")
while True:
line = input()
if line == "/done":
break
lines.append(line)
return "\n".join(lines)
def main():
input_text = get_input()
print("Entered text:")
print(input_text)
if __name__ == "__main__":
main()
you only need to enter /done
on a new line to breakout of the loop and save your paragraph
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论