1

This is my program

from sys import argv
script, first, second, third = argv
print ("the script is:", script)
print("the first variable is:", first)
print("the second variable is:", second)
print ("the third variable is:", third)

The error is:

Traceback (most recent call last):
  File "C:/Users/ravikishore/PycharmProjects/Test/.idea/MyPython.py", line 2, in <module>
    [script, first, second, third] = argv
ValueError: need more than 1 value to unpack
vaultah
  • 44,105
  • 12
  • 114
  • 143
  • The duplicate I linked to is not only the same problem you're having, but it's the same exercise from the same tutorial. – Kevin J. Chase Aug 18 '16 at 02:27
  • As an aside, note that many Python programmers do _not_ recommend _Learn Python the Hard Way_. It has [many problems](http://sopython.com/wiki/LPTHW_Complaints), and there are [better free tutorials](http://sopython.com/wiki/What_tutorial_should_I_read%3F) to choose from. If you don't want to switch, at least remember that there _are_ other tutorials out there, so if a concept in _LPtHW_ doesn't make sense, you can read other explanations to see if they make more sense to you. – Kevin J. Chase Aug 18 '16 at 02:32

2 Answers2

3

argv is a list:

>>> from sys import argv
>>> type(argv)
<type 'list'>

So you're attempting to do a conversion from a list to a tuple, which only works if the number of elements in the tuple exactly matches the list length:

>>> a,b,c = [1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack
>>> a,b,c = [1,2]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack
>>> a,b,c = [1,2,3]
>>> a,b,c = [1,2,3,4]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: too many values to unpack

So you need to add some checks on the argv length prior to attempting the conversion.

Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
2

That code works just fine, assuming that you actually give it three arguments to unpack, as with:

c:\pax> python yourProg.py A B C
the script is: yourProg.py
the first variable is: A
the second variable is: B
the third variable is: C

The problem occurs when you don't give it enough arguments:

c:\pax> python yourProg.py A
Traceback (most recent call last):
  File "yourProg.py", line 2, in <module>
    script, first, second, third = argv
ValueError: not enough values to unpack (expected 4, got 2)

If you want to ensure there are enough arguments before trying to unpack them, you can use len(argv) to get the argument count, and compare that to what you need, something like:

import sys
if len(sys.argv) != 4:
    print("Need three arguments after script name")
    sys.exit(1)
script, first, second, third = sys.argv
print ("the script is:", script)
print("the first variable is:", first)
print("the second variable is:", second)
print ("the third variable is:", third)
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953