Python解包错误需要更多值来解压缩

时间:2015-12-16 22:10:21

标签: python python-2.x

我运行此代码:

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: need more than 1 value to unpack

2 个答案:

答案 0 :(得分:1)

script, first, second, third = argv'将'argv(必须包含4个项目)解包到相应的变量中。显然,你没有将3个参数传递给你的脚本。

请尝试检查:

if len(argv) == 4:
    script, first, second, third = argv
else:
    print "Not enough arguments"

答案 1 :(得分:0)

这意味着您没有为python脚本提供足够的参数。此错误表示您尝试解压缩的值多于列表中的值。像python file.py a b c一样运行。

试试这段代码:

if len(argv) == 4:
    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
else:
    print "Not enough arguments"