input
returns a string. Unpacking will work only if the string has 2 characters:
>>> first, second = 'ot'
>>> first
'o'
>>> second
't'
Otherwise, it will raise an ValueError
:
>>> first, second = 'asdf'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
But that is not what you want. Use str.split
to split the string. Splitting by ,
:
>>> 'hello,world'.split(',')
['hello', 'world']
>>> first, second = 'hello,world'.split(',') # .split(',', 1)
>>> first
'hello'
>>> second
'world'
Splitting by whitespace:
first, second = input('two space-separated numbers:').split(maxsplit=1)