我正在使用Macbook Pro,运行OS X Yosemite 10.10.4,并且正在完成Learn Python Hard Work中的练习。我在IPython的笔记本电脑上运行这些,他们的配置是如下:“ValueError:太多的值解压缩”在学习Python困难的方式,例如13
Python 2.7.10 |Anaconda 2.2.0 (x86_64)| (default, May 28 2015, 17:04:42) [GCC 4.2.1 (Apple Inc. build 5577)]
在EX13上http://learnpythonthehardway.org/book/ex13.html 所列类型和/或复制网站上的确切的代码,但得到了一个错误。
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
在运行上面的代码,我收到错误消息是这样的:
ValueError Traceback (most recent call last) in() ----> 1 script, first, second, third = argv
ValueError: too many values to unpack
我试图运行一行行的代码,并发现问题是,当我指定不止一个值argv。例如,下面的代码完全执行。
from sys import argv
script = argv
print "The script is called:", script
输出上面的代码是:
The script is called: ['/Users/myusername/anaconda/lib/python2.7/site-packages/IPython/kernel/main.py', '-f', '/Users/myusername/.ipython/profile_default/security/kernel-261810c2-9f04-44d4-95f7-411e0db361ff.json', '--profile-dir', '/Users/myusername/.ipython/profile_default']
可能是什么这个可能的原因,我怎么能去纠正呢?
更新: 我试着通过终端运行这个建议,这是我收到的回应。
你应该调用它来自命令行,完全是他们告诉你的方式。这个练习不要使用ipython。 – NightShadeQueen
基本上这个问题是你有4个参数,但你的脚本期望3.试着在你的元组解开之前放一个'print(len(argv))',看看结果是什么。如果那是4以外的任何东西,你将会遇到问题。第二个代码片段将打印整个'argv',而不管参数的数量如何,因此不成问题。 – shuttle87
谢谢,但这是ipython笔记本电脑的限制,代码将通过终端工作,但不是在使用笔记本时? – user2762934