The error is at script, first, second, third = argv
. I would like to understand why I am getting the error and how to fix it.
from sys import argv
script, first, second, third = argv
print("The script is called: ", script)
print("The first variable is: ", first)
print("The second variable is: ", second)
print("The third variable is: ", third)
Verify the assignment variables. If the number of assignment variables is greater than the total number of variables, delete the excess variable from the assignment operator. The number of objects returned, as well as the number of variables available are the same. This will resolve the value error.
Solution. While unpacking a list into variables, the number of variables you want to unpack must equal the number of items in the list. If you already know the number of elements in the list, then ensure you have an equal number of variables on the left-hand side to hold these elements to solve.
We can solve this by ensuring the number of variables equals the number of items in the list to unpack. The error can also happen when trying to iterate over the items in a dictionary. To solve this, you need to use the items() method to iterate over a dictionary.
The “ValueError: not enough values to unpack” error is raised when you try to unpack more values from an iterable object than those that exist. To fix this error, make sure the number of values you unpack from an iterable is equal to the number of values in that iterable.
Run it from the shell like this:
python script.py arg1 arg2 arg3
argv
variable contains command line arguments. In your code you expected 4 arguments, but got only 1 (first argument always script name). You could configure arguments in pycharm
. Go to Run
-> Edit Configurations
. Then create a new python configuration. And there you could specify Script parameters
field. Or you could run your script from command line as mentioned by dnit13.
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