Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
360 views
in Technique[技术] by (71.8m points)

python - ValueError: need more than 1 value to unpack

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:PythonLPTHWexamples_LPTHWex13.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:PythonLPTHWexamples_LPTHWex13c.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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 FiledPython-2.7.1Python.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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...