M4rbleSoda
M4rbleSoda

Reputation: 15

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:

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? My code:

list=[]
l=list
for i in range(0,n):
    l.append(int(raw_input()))

Upvotes: 0

Views: 184

Answers (3)

Amany
Amany

Reputation: 321

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. The next step is to use list comprehension to convert all the numbers in the list "x" into values of type int.

x=raw_input().split(' ') 
x=[int(num) for num in x]
print x #python2

Upvotes: 0

DeepSpace
DeepSpace

Reputation: 81594

You can use split and map:

l = map(int, raw_input().split())

Upvotes: 3

dodopy
dodopy

Reputation: 169

you can use split and list comprehension `

nums = [int(n) for n in raw_input().split(' ')]

Upvotes: 0

Related Questions