Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with script
#1
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
Reply
#2
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...
Reply
#3
Hi snippsat
Big thanks for the help
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020