I'm currently going through Learn Python The Hard Way. I think this example might be out dated so I wanted to get feedback here on it.
I'm using Python 3.1
from sys import argv
script, first, second, third = argv
print("the script is called:", (script))
print("your first variable is:", (first))
print("your second variable is:", (second))
print("your third variable is:", (third))
I'm getting this error:
Traceback (most recent call last):
File "/path/ch13.py", line 3, in <module>
script, first, second, third, bacon = argv
ValueError: need more than 1 value to unpack
Any idea what's wrong?
I think this example will help you. You can pass the number of arguments you want, that is, the number of parameters is variable. :D
Execute the code like this:
You're trying to unpack the
argv
into separate values. Unpacking requires, that the exact amount of values is matched by the size of the value to unpack. Consider this:works fine, but this:
will give you the same ugly error, that you just produced. Unpacking sys.argv in the way you did is especially bad, because it's user input, and you don't know, how many arguments the users of your script will supply. So you should unpack it more carefully:
You can do
and pass 3 arguments
when you run it from command line.
I am using Python 3.6.0. Before i was not wrapping the
argv
arguments in braces. But now it works.All you have to do is type any three things when opening the script. For example, run python (then your filename.py) 1 2 3. The "1, 2 and 3" can be replaced with any three numbers or words.
You forgot to pass arguments to the script, e.g.
foo.py bar baz quux
.