Reputation: 23
I have a number n and I need to get from a user n variables in one line.
As far as i know, it's pretty easy to do if you know exactly how many variables you have.
*variables* = map(int, input().split())
But if I don't know how many variables there are, what should I do?
Also, I'm asked to put the result in array.
Unfortunately, I've just started learning Python, so I have absolutely no idea how to do it and can't show any code I've tried.
Upvotes: 1
Views: 172
Reputation: 26600
User input being taken as a space separated string:
1 2 3 4 5
This being the code you are dealing with:
map(int, input().split())
Stating you need a list, then just store it in a single variable:
inputs = map(int, input().split())
However, dealing with Python 3, you will end up with map
object. So if you actually need a list
type, then just call list
on the map
function:
inputs = list(map(int, input().split()))
Demo:
>>> inputs = list(map(int, input().split()))
1 2 3 4 5
>>> type(inputs)
<class 'list'>
>>> inputs
[1, 2, 3, 4, 5]
Upvotes: 2