Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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?

share|improve this question
2  
The third one would probably be the best if you are taking in more than two inputs. –  SuperJedi224 yesterday
    
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?) –  Alex A. yesterday
    
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 yesterday
    
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 14 hours ago

3 Answers 3

up vote 6 down vote accepted

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)]
share|improve this answer
1  
Doing map(input,'xxx') is equal-length for 3 vars, better for 2, and worse for 4+. –  xnor yesterday
1  
If, by some chance, you need to take exactly 22 inputs, map(input,`id`) is shorter :-). Similarly, map(input,`.1`) for 19 inputs. –  xnor yesterday
1  
Actually, a,b,c=map(input,`.5`) does save a char for 3 inputs, since .5 is represented as 0.5. –  xnor yesterday
    
Wow, that's fascinating. Any explanation on how it works? –  DJ McMayhem 7 hours ago
    
Yeah, that's good. Cool trick! –  DJ McMayhem 5 hours ago

For 3 or more inputs, this is shorter:

a,b,c=eval("input(),"*3)
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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