如何在Spyder中使用argv

发布于 2021-01-29 15:04:02

我正在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
关注者
0
被浏览
70
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    阅读页面底部的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...'
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看