Is there a shorter way to get user input in python?

17

1

This is not a challenge. I'm wondering if it's at all possible to get user input into two separate variables in python (2 or 3) with less than 19 bytes. These are all the shortest I can get:

a,b=input(),input()
a=input();b=input() (newline replaced with semicolon for readability)
i=input;a,b=i(),i()

Is there any shorter way to do this?

James

Posted 2015-05-27T17:12:32.823

Reputation: 54 537

2The third one would probably be the best if you are taking in more than two inputs. – SuperJedi224 – 2015-05-27T17:26:50.520

1I was going to say command line arguments with a,b=sys.argv[1:2] or whatever but then you need an import sys (right?) – Alex A. – 2015-05-27T17:31:41.253

In python3 a,b=input().split() would be another 19 bytes solution, not an improvement though. Note that that means a and b are strings and the input is entered in one go seperated by a space. – Matty – 2015-05-27T17:34:39.883

Not sure if it helps, but in some languages you could use a vector to get two numbers, rather than 2 variables. For two arbitrary variables a more complicated container may be used. – Dennis Jaheruddin – 2015-05-28T07:56:48.510

a,b=input() in Py2 – Oliver Ni – 2016-11-23T16:35:50.110

Answers

11

If prompting the user doesn't matter, this is slightly shorter than eval for 3 or more:

a,b,c=map(input,[1]*3)
a,b,c=map(input,'111')

It's also shorter even if prompting is not ok:

a,b,c=map(input,['']*3)

Sadly, xnor's idea doesn't save anything for 2 inputs as it is still longer (19 bytes). However if you have some string (or appropriate iterable) with the appropriate length already (because magic?) then it could be shorter.

xnor found an example of a magic string for 3 inputs in Python 2 only:

a,b,c=map(input,`.5`)

This also requires that the prompts not be important, though.

This trick actually helps to save bytes for large numbers of inputs by using 1eX notation (getting x+3 inputs):

 a,b,c,d,e,f=map(input,`1e3`)

Explanation:

Python's map function performs the function it is passed on each element of the iterable that is also passed to it, and returns the list (or map object) containing the results of these calls. For the first example, the map could be decomposed to:

[input(1), input(1), input(1)]

FryAmTheEggman

Posted 2015-05-27T17:12:32.823

Reputation: 16 206

2Doing map(input,'xxx') is equal-length for 3 vars, better for 2, and worse for 4+. – xnor – 2015-05-27T20:42:12.780

3If, by some chance, you need to take exactly 22 inputs, map(input,`id`) is shorter :-). Similarly, map(input,`.1`) for 19 inputs. – xnor – 2015-05-27T21:15:36.103

3Actually, a,b,c=map(input,`.5`) does save a char for 3 inputs, since .5 is represented as 0.5. – xnor – 2015-05-27T21:19:50.110

Yeah, that's good. Cool trick! – James – 2015-05-28T17:26:01.730

6

For 3 or more inputs, this is shorter:

a,b,c=eval("input(),"*3)

Keith Randall

Posted 2015-05-27T17:12:32.823

Reputation: 19 865

4

Note that unless the question specifies the input type or requires a complete program, you can take input as function arguments, cutting out the need for input(). Where this is not an option, the following may help in certain cases.


If you want single characters in the variables a and b, you can use

a,b=input()

Entering a 2 character string will then result in the first character being assigned to a and the second character being assigned to b. (Less than 2 or more than 2 characters in the string will give an error.)

If you require more than a single character per variable then this approach will not work. This could still be used to enter 2 single digit numbers as a string. If numbers beyond 9 are required then the full range of potential input characters could be interpreted as numbers.

Note that this also works for more than 2 variables:

a,b,c,d,e,f,g,h=input()

This works because in Python a string is a sequence of characters, and can be used anywhere a sequence is expected.

trichoplax

Posted 2015-05-27T17:12:32.823

Reputation: 10 499