[英]How do I make my code add new inputs on a new line in a document while using the .writelines in python?
[英]How do i make multiline inputs using python?
我刚刚学习 python 并且几乎没有经验,我遇到了一些问题。 我正在学习编辑文本文件,我想看看我是否可以在运行时将多行文本复制到 input() function 中,或者如果你不能这样做。 如果这很重要,我也在使用 Replit。 这是我的代码:
example = input("Some multi-line text: ")
print(example)
我也尝试过使用:
example = input("""Some multi-line text: """)
print(example)
但它没有用。 然后我尝试在 shell 中输入 \n(记住我正在使用 Replit)。 那也没用,它只是打印了:example \n example
您可以尝试使用以下 Python 片段进行多行处理。
delimiter = ''
for line in iter(input, delimiter):
<do something here>
您可以对其进行编码,以便在添加到user_text
时不断地输入一个 while 循环,并在末尾添加一个新行。 您有一个条件检查某个关键字以停止循环。
user_text = ""
while True:
new_text = input()
if new_text == "end":
break
user_text += f"{new_text}\n"
print(user_text)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.