如何在Python 3中获取多行代码的用户输入? [英] How to get user input for multiline lines in Python 3?

查看:1249
本文介绍了如何在Python 3中获取多行代码的用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总结newb在这个,并试图弄清楚这一点。我知道常规输入函数可以接受
单行,但只要您尝试编写一个字符串段并敲下一行的输入,它就会终止。是否有初学者友好的方式接受多行用户字符串输入作为变量?谢谢!

解决方案

在程序中执行此操作的常用方法是使用我完成字符串期间),并继续阅读,直到读取的行匹配该字符串。

  print(输入尽可能多的文本行)
print(完成后,单独输入一个单独的句点。)

buffer = []
True:
print(>,end =)
line = input()
if line ==。:
break
buffer.append( )
print()
print(multiline_string)
multiline_string =\\\
.join(buffer)

print(You entered ... )


Total newb at this and tried to figure this out. I know that the regular input function can accept single lines, but as soon as you try to write a string paragraph and hit enter for the next line, it terminates. Is there a beginner friendly way to accept multiline user string inputs as variables? Thanks!

解决方案

A common way of doing this in programs is to have an "I'm done" string (e.g. a single period), and to keep reading in lines until the line read matches that string.

print("Enter as many lines of text as you want.")
print("When you're done, enter a single period on a line by itself.")

buffer = []
while True:
    print("> ", end="")
    line = input()
    if line == ".":
        break
    buffer.append(line)
multiline_string = "\n".join(buffer)

print("You entered...")
print()
print(multiline_string)

这篇关于如何在Python 3中获取多行代码的用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆