[英]How do I read multiple inputs in python 2
I am new to python.我是 python 的新手。 I searched and found out how to do it in python 3:
我在 python 3 中搜索并发现了如何做到这一点:
v = [int(x) for x in input().split()]
but in python 2 I get the syntax error.但是在 python 2 中,我得到了语法错误。 I need it for foobar because I can't code in c++ nor python 3 there which is incredibly annoying.
我需要它用于 foobar,因为我无法在 c++ 和 python 3 中编码,这非常烦人。
In Python 2 you have 2 input functions:在 Python 2 中,您有 2 个输入功能:
raw_input
: this does what you expect; raw_input
:这符合您的期望; it reads the input as a string.input
: this actually evaluates the user input; input
:这实际上评估了用户输入; this is why you get a syntax error;"1 2 3 4 5"
."1 2 3 4 5"
这样的字符串。 So you want to use raw_input()
:所以你想使用
raw_input()
:
v = [int(x) for x in raw_input().split()]
In Python 3 input
instead does what you want.在 Python 3
input
代替你想要的。
Try this:尝试这个:
v = [int(x) for x in raw_input("Enter your numbers separated by space: ").split()]
raw_input()
takes exactly what the user typed and passes it back as a string raw_input()
获取用户输入的内容并将其作为字符串传回input()
first takes the raw_input()
and then performs an eval()
on as well. input()
首先接受raw_input()
然后执行eval()
。raw_input()
was renamed to input()
so now input()
returns exactly what the user typed and passes it back as a string raw_input()
被重命名为input()
所以现在input()
返回用户输入的内容并将其作为字符串传回input()
was removed.input()
已被删除。In python3 there is no raw_input
.在 python3 中没有
raw_input
。 The python3 input
is the renamed python2 raw_input
. python3
input
是重命名的 python2 raw_input
。 So in python2 you should use raw_input
instead.所以在 python2 中你应该使用
raw_input
。 Like this:像这样:
v = [int(x) for x in raw_input("Enter numbers here").split(" ")]
The argument in split
is the separator. split
中的参数是分隔符。 In the first example the separator is a space.在第一个示例中,分隔符是空格。 But if you want to get the numbers separated with
-
, you can use it like this:但是如果你想用
-
分隔数字,你可以像这样使用它:
v = [int(x) for x in raw_input("Enter numbers here").split("-")]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.