Need more than 1 value to unpack?

Started by
7 comments, last by Sky Warden 10 years, 4 months ago

Hi everyone i just learning execise13 from learnpythonthehardway.org and i got this error:

Value error:Need more than 1 value to unpack

And this is my script:

from sys import argv

script, first, second, third = argv

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

Is there anything wrong in my script thank you very muchbiggrin.png

Advertisement

Can you tell me how you call the script? The script takes three extra argumens (first, second, third). From what I get, you're supposed to call it like this:


python myscript.py somethingforfirst somethingforsecond somethingforthird

Those are called arguments. Those arguments are first taken and packed into argv. In this line:


script, first, second, third = argv

You're unpacking stuff inside argv into separate variables. How do I explain it? Just say argv is a list (array, whatever). The content of argv is like:


['myscript.py', 'somethingforfirst', 'somethingforsecond', 'somethingforthird']

When you unpack it, those items in the array go to the variables in order, so 'myscipt.py' goes to script, 'somethingforfirst' goes to first, and the rest goes like that.

The error you see is caused because you try to take more than argv actually has. You're likely to only pass one argument ('myscript.py'), while you try to unpack argv into four variables. Python simply tells you that she needs more items to unpack.

Try to make a simple list of five items, and try to unpack it into six variables. You will get the same error message.

try this:


from sys import argv
for arg in argv:
 print(arg)

and you will see that there is a argv in your case

also you can check length of argv before unpacking


if len(argv) > 1:
 #do some code here
Is there anything wrong in my script thank you very much

You have to call it with at least 3 arguments!

E.g:

If you execute this:


$ python ex13.py first 2nd 3rd

You should see this:


The script is called: ex13.py
Your first variable is: first
Your second variable is: 2nd
Your third variable is: 3rd

Can you tell me how you call the script? The script takes three extra argumens (first, second, third). From what I get, you're supposed to call it like this:




python myscript.py somethingforfirst somethingforsecond somethingforthird

Those are called arguments. Those arguments are first taken and packed into argv. In this line:




script, first, second, third = argv

You're unpacking stuff inside argv into separate variables. How do I explain it? Just say argv is a list (array, whatever). The content of argv is like:




['myscript.py', 'somethingforfirst', 'somethingforsecond', 'somethingforthird']

When you unpack it, those items in the array go to the variables in order, so 'myscipt.py' goes to script, 'somethingforfirst' goes to first, and the rest goes like that.

The error you see is caused because you try to take more than argv actually has. You're likely to only pass one argument ('myscript.py'), while you try to unpack argv into four variables. Python simply tells you that she needs more items to unpack.

Try to make a simple list of five items, and try to unpack it into six variables. You will get the same error message.

Is there anything wrong in my script thank you very much

You have to call it with at least 3 arguments!

E.g:

If you execute this:




$ python ex13.py first 2nd 3rd

You should see this:




The script is called: ex13.py
Your first variable is: first
Your second variable is: 2nd
Your third variable is: 3rd



I still don't get it when i tried that it give me an error say invalid syntax.Here my script:

from sys import argv

$ python ex13.py first 2nd 3rd

script, first, second, third = argv

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

Thank you for answering my question

try this:




from sys import argv
for arg in argv:
 print(arg)

and you will see that there is a argv in your case

also you can check length of argv before unpacking




if len(argv) > 1:
 #do some code here

Thank you i will try thissmile.png

I still don't get it when i tried that it give me an error say invalid syntax.Here my script:


from sys import argv

$ python ex13.py first 2nd 3rd

script, first, second, third = argv

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

Thank you for answering my question

No. It's not like that. What Renega means with this:


If you execute this:
$ python ex13.py first 2nd 3rd
You should see this:

is not for the script. It's a Linux terminal command.

BDnwKwD.png

While talking about terminal command on the Internet, people tend to use the $ to indicate that "This is run in the terminal," so what Renega is saying is that you run:


python ex13.py first 2nd 3rd

on the Command Prompt (I guess you're running on Windows).

There's nothing wrong with your original script. What caused the problem is that you didn't give enough arguments for the script to unpack (explained in my first post). Let me explain it with more detail. argv is a list, so when you're unpacking it like this:


script, first, second, third = argv

It's basically just like this:


script, first, second, third = 'ex13.py', '1st', '2nd', '3rd'

Because argv is actually like this:


['ex13.py', '1st', '2nd', '3rd']

And it gets those items from those arguments you pass when you call the script on the Command Prompt:


python ex13.py 1st 2nd 3rd

Try to print argv and you will get what I mean. Is it clear now? :)

I still don't get it when i tried that it give me an error say invalid syntax.Here my script:




from sys import argv

$ python ex13.py first 2nd 3rd

script, first, second, third = argv

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

Thank you for answering my question

No. It's not like that. What Renega means with this:


If you execute this:
$ python ex13.py first 2nd 3rd
You should see this:

is not for the script. It's a Linux terminal command.

BDnwKwD.png

While talking about terminal command on the Internet, people tend to use the $ to indicate that "This is run in the terminal," so what Renega is saying is that you run:




python ex13.py first 2nd 3rd

on the Command Prompt (I guess you're running on Windows).

There's nothing wrong with your original script. What caused the problem is that you didn't give enough arguments for the script to unpack (explained in my first post). Let me explain it with more detail. argv is a list, so when you're unpacking it like this:




script, first, second, third = argv

It's basically just like this:




script, first, second, third = 'ex13.py', '1st', '2nd', '3rd'

Because argv is actually like this:




['ex13.py', '1st', '2nd', '3rd']

And it gets those items from those arguments you pass when you call the script on the Command Prompt:




python ex13.py 1st 2nd 3rd

Try to print argv and you will get what I mean. Is it clear now? smile.png

Wow i can do that now.It is really clear thank you for helping me smile.png and sorry because i really hard to understand that biggrin.png

Wow i can do that now.It is really clear thank you for helping me smile.png and sorry because i really hard to understand that biggrin.png

My pleasure.

Don't worry about it. Everyone starts somewhere. Fully understanding these little things in the beginning can save a whole lot of pain down the road. There are actually people who don't care about how exactly their program runs as long as it's working, and when things go wild, they can't find the source of the problem, so whenever you feel unsure about how a piece of code works, track it down until it's clear. You can always ask, of course. That's what this community is for.

Study hard!

This topic is closed to new replies.

Advertisement