Richa
Richa

Reputation: 161

Using arguments in python - value error

I am new to python programming and I tried this code:

from sys import argv


script, first, second, third = argv
print "this script is called:" ,script

print "the first variable is called : " ,first

print "the second variable is called : ", second

print "the third variable is called : " ,third

I am getting the error :

Traceback (most recent call last):
  File "/Users/richavarma/Documents/first.py", line 4, in <module>
    script, first, second, third = argv
ValueError: need more than 1 value to unpack

My output should be as follows:

this script is called: abc.py
the first variable is called: first
the second variable is called : second
the third variable is called : third

Upvotes: 1

Views: 264

Answers (1)

fileyfood500
fileyfood500

Reputation: 1321

In short, argv takes arguments from the command line. If you type this command into the command line:

python test.py first second third

You will pass 4 arguments to your python code: test.py, first, second, and third You can take in all 4 arguments as inputs by assignment like so:

from sys import argv

(filename, arg1, arg2, arg3) = argv

After this you can use any of the arguments as strings with their variable names.

Upvotes: 1

Related Questions