tags:

views:

442

answers:

3

I run this code:

from sys import argv

script, user_name =argv
prompt = '>'

print "Hi %s, I'm the %s script." % (user_name, script)

And I get the following error:

Traceback (most recent call last):
script, user_name =argv
ValueError: need more than 1 value to unpack

+7  A: 

Probably you didn't provide an argument on the command line. In that case, sys.argv only contains one value, but it would have to have two in order to provide values for both user_name and script.

David Zaslavsky
+2  A: 

You can't run this particular piece of code in the interactive interpreter. You'll need to save it into a file first so that you can pass the argument to it like this

$ python hello.py user338690
gnibbler
+1  A: 

You shouldn't be doing tuple dereferencing on values that can change like your line below.

 script, user_name = argv

The line above will fail if you pass less than one argument or more than one argument. A better way of doing this is to do something like this:

 for arg in argv[1:]:
     print arg

Of cause you will do something other than print the args. Maybe put a series of 'if' statement in the 'for' loop that set variables depending on the arguments passed. An even better way is to use the getopt or optparse packages.

cnobile