I am familiar with the input() function, to read a single variable from user input. Is there a similar easy way to read two variables?
I'm looking for the equivalent of:
scanf("%d%d", &i, &j); // accepts "10 20\n"
One way I am able to achieve this is to use raw_input()
and then split
what was entered. Is there a more elegant way?
This is not for live use. Just for learning..
No, the usual way is
raw_input().split()
In your case you might use
map(int, raw_input().split())
if you want them to be integers rather than stringsDon't use
input()
for that. Consider what happens if the user entersFirstly read the complete line into a string like
Then use a for loop like this
This loop takes a full line as input to the string and processes the parts in it individually and then appends the numbers to the list - lst after converting them to integers .
or you can do this
You can also read from sys.stdin
You can also use this method for any number of inputs. Consider the following for three inputs separated by whitespace:
you can read 2 int values by using this in python 3.6.1