无法弄清如何使回车键不提交 input()。

huangapple go评论53阅读模式
英文:

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

huangapple
  • 本文由 发表于 2023年6月15日 20:40:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定