小编典典

如何在Spyder中使用argv

python

我正在Spyder中运行以下代码。我已经在py文件中输入了内容,只需点击运行按钮即可。

当我尝试运行它时,出现错误:

ValueError:需要多个值才能解压

如此处所示,您打算在运行程序之前为argv变量提供输入,但我不知道这是怎么做的?

http://learnpythonthehardway.org/book/ex13.html

from sys import argv

script, first, second, third = argv

print "The script is called:", script
print "The first variable is:", first
print "The second variable is:", second
print "Your third variable is:", third

阅读 193

收藏
2021-01-20

共1个答案

小编典典

阅读页面底部的FAQ,它专门提到了此错误。

常见学生问题

问: 当我运行它时,我得到ValueError: need more than 1 value to unpack

请记住,一项重要技能是注意细节。如果查看“应该看到的内容”部分,则会看到我在命令行上运行带有参数的脚本。您应该复制我精确运行的方式。

确保运行命令:

$ python ex13.py first 2nd 3rd


>> The script is called: ex13.py  
>> Your first variable is: first  
>> Your second variable is: 2nd  
>> Your third variable is: 3rd

您可以确保提供了参数。

if __name__ == '__main__':
    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 'You forgot the args...'
2021-01-20