-3

I am trying to learn how to code in Python, whenever I do a exercise involving from sys import argv I get this error code:

Traceback (most recent call last): File "C:/Users/Kaleb/PycharmProjects/python ex13.py", line 3, in script, first, second, third = argv ValueError: need more than 1 value to unpack

This is the code im trying to do:

from sys import argv

script, first, second, third = argv

print "The script is called:", script
print "Your first variable is:", first
print "Your second variable is:", second
print "Your third variable is:", third

I am not sure why it is doing this and im very new to coding. I had had a look around but I cannot find anything helpful or that I can understand.

What does this error mean and what do I need to do to fix it?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Kaleb Tempest
  • 11
  • 1
  • 2

2 Answers2

1

You're trying to assign variables to non-existent values

script, first, second, third = argv

When you call your script you need to pass in three parameters as well

So you can call your script like this:

python ex13.py test1 test2 test3

You should see:

The script is called: ./ex13.py                                                                            
Your first variable is: test1                                                                               
Your second variable is: test2                                                                              
Your third variable is: test3
Mike
  • 3,830
  • 2
  • 16
  • 23
0

It works perfectly fine if you call it correctly. Assuming you script is called output.py you need to run it like this:

python output.py z u i

where z, u and i are three arguments which you try to unpack. Then you receive:

The script is called: output.py
Your first variable is: z
Your second variable is: u
Your third variable is: i

If you call it without these three arguments just using

python output.py

you receive the error message you mentioned:

Traceback (most recent call last):
  File "output.py", line 3, in <module>
    script, first, second, third = argv
ValueError: need more than 1 value to unpack
Cleb
  • 25,102
  • 20
  • 116
  • 151