Rakibul Hasan
Rakibul Hasan

Reputation: 108

Problem in taking multiple input in a line

I want to take input like + 2 3 ? 1 20 where the first variable is char and the next will be integers

I have done this

sign,m,n = input().split()
[sign,m,n]=[str(sign),int(m),int(n)]

But i get this error

ValueError: not enough values to unpack (expected 3, got 0)

Upvotes: 2

Views: 59

Answers (3)

najayaz
najayaz

Reputation: 194

Just adding the enclosing square paranthesis in your first statement works on Python 3.6.7 :

[GCC 8.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> [sign,m,n] = input().split()
- 2 3
>>> [sign,m,n] = [str(sign),int(m),int(n)]
>>> sign
'-'
>>> m
2
>>> n
3

Upvotes: 0

hygull
hygull

Reputation: 8740

If you want to use your own logic to get it done, then here is the way.

Just append [:3] at the end of first statement to slice the list.

>>> sign, m, n = input().split()[:3]
+ 2 3 ? 1 20
>>>
>>> [sign,m,n] = [str(sign), int(m), int(n)]
>>>
>>> sign
'+'
>>>
>>> m
2
>>>
>>> n
3
>>>

And here is another way to accomplish the same in a single line.

For this you can use the concept of list comprehension.

>>> sign, m, n = [c if i == 0 else int(c) for i, c in enumerate(input().split()[:3])]
+ 2 3 ? 1 20
>>>
>>> sign
'+'
>>> m
2
>>> n
3
>>>

Upvotes: 0

Agapito Gallart Bernat
Agapito Gallart Bernat

Reputation: 363

Your problem lies in sign,m,n = input().split(). You have to treat as a list, not as function which returns 3 values. Here it is a snippet code of what you desire:

stdin = input().split()
sign,m,n = str(stdin[0]),int(stdin[1]),int(stdin[2])

Stdin is a list

Upvotes: 1

Related Questions