Jump to content

  • Log In with Google      Sign In   
  • Create Account


Need more than 1 value to unpack?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Kurai Tsubasa   Members   -  Reputation: 199

Like
0Likes
Like

Posted 01 December 2013 - 06:08 AM

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



Sponsor:

#2 Herwin P   Members   -  Reputation: 631

Like
4Likes
Like

Posted 01 December 2013 - 07:47 AM

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.


Edited by Herwin Pradana, 01 December 2013 - 09:20 PM.


#3 pyirrlicht   Members   -  Reputation: 618

Like
1Likes
Like

Posted 01 December 2013 - 09:57 AM

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

Edited by pyirrlicht, 01 December 2013 - 10:02 AM.


#4 ColinDuquesnoy   Members   -  Reputation: 1102

Like
1Likes
Like

Posted 02 December 2013 - 02:26 AM

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

					
					

#5 Kurai Tsubasa   Members   -  Reputation: 199

Like
0Likes
Like

Posted 02 December 2013 - 04:49 AM

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



#6 Kurai Tsubasa   Members   -  Reputation: 199

Like
1Likes
Like

Posted 02 December 2013 - 04:50 AM

 

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 



#7 Herwin P   Members   -  Reputation: 631

Like
2Likes
Like

Posted 02 December 2013 - 07:54 PM

 

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? :)



#8 Kurai Tsubasa   Members   -  Reputation: 199

Like
0Likes
Like

Posted 03 December 2013 - 03:59 AM

 

 

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 



#9 Herwin P   Members   -  Reputation: 631

Like
1Likes
Like

Posted 03 December 2013 - 04:24 AM

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!






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS