Hello!
When you ask for an input what you get is a single string object. What is happening when Python unpack the numbers is to attempt to assign each symbol to the variables. If you type
Here is what is happening:
When you ask for an input what you get is a single string object. What is happening when Python unpack the numbers is to attempt to assign each symbol to the variables. If you type
1 2
as an input, the length of the string is 3 - '1', ' ', '2'. So you have three values to unpack to two variables. You have two options. To add split() at the end of the input(). To get the string and unpacking it after the input.Here is what is happening:
In [1]: a, b = input("Enter two numbers> ") Enter two numbers> 1 2 # three symbols
Error:ValueError Traceback (most recent call last)
<ipython-input-1-88647334f199> in <module>()
----> 1 a, b = input("Enter two numbers> ")
ValueError: too many values to unpack (expected 2)
In [2]: a, b = input("Enter two numbers> ").split() Enter two numbers> 1 2 # three symbols In [3]: a, b = input("Enter two numbers> ") Enter two numbers> 12 # two symbols In [4]: print(a, b) 1 2Still, the type of the a and b is string. You have to int() it.