Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ValueError: need more than 1 value to unpack

Tags:

python

Disclaimer: I have read the thread located at Python Error: "ValueError: need more than 1 value to unpack", and none of the answers explained how to actually get the code to run.

I am reading Learn Python the Hard Way, and I am on Exercise 13.

Here is the code I am attempting to run in the IDLE thing (don't know what it is really called) for Python 2.7:

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

When run, I get the error:

"Traceback (most recent call last): File "C:\Python\LPTHW\examples_LPTHW\ex13.py", line 3, in script, first, second, third = argv ValueError: need more than 1 value to unpack"

I have tried running the program through the Windows command prompt, to no avail. I have also run the program by typing: "python ex13.py first 2nd 3rd", in both the IDLE and the command prompt, also got the same error.

How do I get this code to run, and what the heck am I doing wrong?

EDIT: @John Machin, I made a program exactly like the one you posted, saved it as a .py, then went into the correct directory where my file was saved, ran the program using "python yourcode.py B C D" and I received the following message "python is not recognized as an internal or external command..." When I run the program by just typing it's name (which works for all other programs besides this one) I get an error saying "SyntaxError: invalid syntax on line 2: print len(argv), repr(argv)".

EDIT 2 (With the new code): @John Machin, I copied your program word for word into gedit and made the .py file as usual. I opened my terminal (command prompt) and typed:

python ex13c.py B C D

And received:

'python' is not recognized as an internal or external command, operable program or batch file."

Then I typed:

ex13c.py

And received:

3.1.3 (r313:86834, Nov 27 2010, 18:30:53) [MSC v.1500 32 bit (Intel)]
1 ['C:\\Python\\LPTHW\\examples_LPTHW\\ex13c.py']
Traceback (most recent call last):
  File "C:\Python\LPTHW\examples_LPTHW\ex13c.py". line 4, in (module)
    a, b, c, d = argv
ValueError: need more than 1 value to unpack

Keep in mind I am running W7 64bit, and that Zed is teaching in 2.X and I have been using 2.7 the entire book so far. Every other example has worked. Right before I started Example 11, I installed 3.1 for a different book I am about to start reading. Every time I use the IDLE included with 2.7. Perhaps it is possible I am somehow inadvertently using 3.1 while trying to code in 2.7?

Thanks for bearing with me.

like image 648
rayne117 Avatar asked Dec 22 '10 08:12

rayne117


1 Answers

The example works. It seems like you aren't running it correctly.

Firstly, IDLE is a somewhat special environment that I never saw the point of, just run it with the normal Python interpreter.

Secondly, specify the whole path to your python interpreter. Instead of

 python ex13c.py B C D

You on Windows need to do something like this:

 "C:\Program Filed\Python-2.7.1\Python.exe" ex13c.py B C D

Reading exercise 13 I also see that Zed calls modules "features" up until chapter 13. There is no point in doing that, that's just confusing. He also in some chapter tells people to memorize the output of logical tables, which is nonsense. You don't need to memorize them, you need to understand them.

Each time this book pops up in questions here I get less impressed with it. I'm sure there must be a better tutorial. Perhaps Dive into Python?

like image 137
Lennart Regebro Avatar answered Oct 16 '22 16:10

Lennart Regebro