这是我的程序
from sys import argv
script, first, second, third = argv
print ("the script is:", script)
print("the first variable is:", first)
print("the second variable is:", second)
print ("the third variable is:", third)
错误是:
Traceback (most recent call last):
File "C:/Users/ravikishore/PycharmProjects/Test/.idea/MyPython.py", line 2, in <module>
[script, first, second, third] = argv
ValueError: need more than 1 value to unpack
argv
是一个列表:
>>> from sys import argv
>>> type(argv)
<type 'list'>
因此,您正在尝试将列表转换为元组,只有当元组中的元素数量完全与列表长度匹配时才有效:
>>> a,b,c = [1]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack
>>> a,b,c = [1,2]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack
>>> a,b,c = [1,2,3]
>>> a,b,c = [1,2,3,4]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: too many values to unpack
因此您需要在尝试转换之前对 argv
长度添加一些检查。