[英]How can I take raw input from only one line?
I want to type input from my keyboard in the PowerShell like this: 1 2 3 4 but if I type it like that, it will show this: 我想像这样在PowerShell中从键盘输入输入:1 2 3 4但如果这样输入,它将显示如下:
Traceback (most recent call last):
File "iqtest.py", line 7, in <module>
l.append(int(raw_input()))
ValueError: invalid literal for int() with base 10: '1 2 3 4'
It only works when I enter after each input, thus creating 4 lines instead of 1. How can I take all 4 input just in one line? 仅当我在每次输入后输入时才起作用,因此创建了4行而不是1行。如何将全部4个输入仅放在一行中? My code:
我的代码:
list=[]
l=list
for i in range(0,n):
l.append(int(raw_input()))
Here I am reading multiple numbers separated by space So I am using .split(' ') which will return a list of numbers but of type string. 在这里,我正在读取多个以空格分隔的数字,所以我在使用.split(''),它将返回数字列表,但类型为string。 The next step is to use list comprehension to convert all the numbers in the list "x" into values of type int.
下一步是使用列表推导将列表“ x”中的所有数字转换为int类型的值。
x=raw_input().split(' ')
x=[int(num) for num in x]
print x #python2
您可以使用split
和list comprehension
`
nums = [int(n) for n in raw_input().split(' ')]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.