在python中使用参数-值错误

在python中使用参数-值错误,python,Python,我是python编程新手,我尝试了以下代码: from sys import argv script, first, second, third = argv print "this script is called:" ,script print "the first variable is called : " ,first print "the second variable is called : ", second print "the third variable is c

我是python编程新手,我尝试了以下代码:

from sys import argv


script, first, second, third = argv
print "this script is called:" ,script

print "the first variable is called : " ,first

print "the second variable is called : ", second

print "the third variable is called : " ,third
我得到一个错误:

Traceback (most recent call last):
  File "/Users/richavarma/Documents/first.py", line 4, in <module>
    script, first, second, third = argv
ValueError: need more than 1 value to unpack

简而言之,argv从命令行获取参数。如果在命令行中键入此命令:

python test.py first second third
您将向python代码传递4个参数:test.py、first、second和third 您可以通过赋值将所有4个参数作为输入,如下所示:

from sys import argv

(filename, arg1, arg2, arg3) = argv

在此之后,您可以使用任何参数作为字符串及其变量名。

您能告诉我们如何执行脚本吗?您的程序在我这端工作:
python untitled.py 1 2 3此脚本名为:untitled.py第一个变量名为:1第二个变量名为:2第三个变量名为:3
my我猜这个错误意味着您没有传入任何命令行参数。如果我是你,我会添加一个守卫来检查传入的参数的数量,以便优雅地退出。你能告诉我们你是如何执行你的脚本的吗?@Richa这就是问题所在
sys.argv
需要命令行选项。你不能在空闲时这样做。尝试使用命令行参数从shell运行它(如rth的示例中),请发布返回错误的终端命令行。在我这边,一切都正常。
from sys import argv

(filename, arg1, arg2, arg3) = argv