In Python, when I run this code:
from sys import argv script, user_name =argv prompt = '>' print "Hi %s, I'm the %s script." % (user_name, script)
I get this error:
Traceback (most recent call last): script, user_name =argv ValueError: need more than 1 value to unpack
What does that error mean?
We can resolve the error by using a method called items() . The items() function returns a view object which contains both key-value pairs stored as tuples. Note: If you are using Python version 2. x, you need to use iteritems() instead of the items() function.
The ValueError: too many values to unpack (expected 2)” occurs when you do not unpack all of the items in a list. A common mistake is trying to unpack too many values into variables. We can solve this by ensuring the number of variables equals the number of items in the list to unpack.
During a multiple value assignment, the ValueError: not enough values to unpack occurs when either you have fewer objects to assign than variables, or you have more variables than objects. This error caused by the mismatch between the number of values returned and the number of variables in the assignment statement.
Probably you didn't provide an argument on the command line. In that case, sys.argv
only contains one value, but it would have to have two in order to provide values for both user_name
and script
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With