17
\$\begingroup\$

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?

\$\endgroup\$
  • 2
    \$\begingroup\$ The third one would probably be the best if you are taking in more than two inputs. \$\endgroup\$ – SuperJedi224 May 27 '15 at 17:26
  • 1
    \$\begingroup\$ I was going to say command line arguments with a,b=sys.argv[1:2] or whatever but then you need an import sys (right?) \$\endgroup\$ – Alex A. May 27 '15 at 17:31
  • \$\begingroup\$ 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. \$\endgroup\$ – Matty May 27 '15 at 17:34
  • \$\begingroup\$ 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. \$\endgroup\$ – Dennis Jaheruddin May 28 '15 at 7:56
  • \$\begingroup\$ a,b=input() in Py2 \$\endgroup\$ – Oliver Ni Nov 23 '16 at 16:35
11
\$\begingroup\$

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)]
| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ Doing map(input,'xxx') is equal-length for 3 vars, better for 2, and worse for 4+. \$\endgroup\$ – xnor May 27 '15 at 20:42
  • 3
    \$\begingroup\$ If, by some chance, you need to take exactly 22 inputs, map(input,`id`) is shorter :-). Similarly, map(input,`.1`) for 19 inputs. \$\endgroup\$ – xnor May 27 '15 at 21:15
  • 3
    \$\begingroup\$ Actually, a,b,c=map(input,`.5`) does save a char for 3 inputs, since .5 is represented as 0.5. \$\endgroup\$ – xnor May 27 '15 at 21:19
  • \$\begingroup\$ Yeah, that's good. Cool trick! \$\endgroup\$ – James May 28 '15 at 17:26
6
\$\begingroup\$

For 3 or more inputs, this is shorter:

a,b,c=eval("input(),"*3)
| improve this answer | |
\$\endgroup\$
4
\$\begingroup\$

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.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.