Python Forum

Full Version: Help with script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have just started to learn Python and i bought the book 'Learn Python 3 the Hard Way'. Well every thing was fine until i came to exercise 13 and it was called Parameters, Unpacking, Variables. All i had to do was copy the following:

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)
Save it as ex13.py and then run it. I used the Atom text editor.
But no matter what or how many times i've checked and re-checked i always get
an error message when i run it. The error message is as follows:

$ oldDog@-linux-desktop:~$ python3.6 ~/python_scripts/ex13.py
File "/home/oldDog/python_scripts/ex13.py", line 3, in <module>
script, first, second, third = argv
ValueError: not enough values to unpack (expected 4, got 1)
$ oldDog@-linux-desktop:~$

Could someone please show me what i've done wrong.

Big thanks
sys.argv is a way to get command line arguments into python.
ex13.py 1 2 3 should do it.
Test:
λ python foo.py 1 2 3
The script is called: foo.py
The first variable is: 1
The second variable is: 2
The third variable is: 3
Or string:
λ python foo.py hello world car
The script is called: foo.py
The first variable is: hello
The second variable is: world
The third variable is: car
sys.argv is the simplest version of this,argparse is in standard library.
There also many 3-part librarians for this Click(my favorite),Dotcop,Fire ect...
Hi snippsat
Big thanks for the help