如何在 Spyder 中使用 argv [英] How to use argv with Spyder

查看:80
本文介绍了如何在 Spyder 中使用 argv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Spyder 中运行下面的代码.我已将其输入到 py 文件中,然后只需点击运行按钮即可.

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

ValueError: 需要 1 个以上的值来解包

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

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

from sys import argv脚本,第一,第二,第三 = argvprint "脚本被调用:", 脚本打印第一个变量是:",第一个print "第二个变量是:", second打印您的第三个变量是:",第三个

解决方案

阅读页面底部的 FAQ,它特别提到了这个错误.

<块引用>

学生常见问题

问.当我运行它时,我得到 ValueError:需要超过 1 个值来解压.

请记住,关注细节是一项重要技能.如果您查看您应该看到的内容部分,您会看到我在命令行上运行带有参数的脚本.你应该完全复制我的运行方式.

确保您运行命令:

<块引用>

$ python ex13.py first 2nd 3rd

<块引用>

<代码>>>脚本名为:ex13.py>>你的第一个变量是:第一>>你的第二个变量是:2nd>>你的第三个变量是:3rd

您可以确保提供了参数.

如果 __name__ == '__main__':如果 len(argv) == 4:脚本,第一,第二,第三 = argvprint '脚本被调用:', 脚本打印 '你的第一个变量是:', 首先print '你的第二个变量是:', second打印 '你的第三个变量是:', 第三个别的:打印你忘记了参数......"

I'm running the code below in Spyder. I have typed it in a py file and simply hit the run button.

When I try to run it I get the error:

ValueError: need more than 1 value to unpack

As shown here you are meant to give the inputs for the argv variable before running the program but I don't know how to do this is spyder?

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

解决方案

Read the FAQ at the bottom of the page, it specifically mentions this error.

Common Student Questions

Q. When I run it I get ValueError: need more than 1 value to unpack.

Remember that an important skill is paying attention to details. If you look at the What You Should See section you see that I run the script with parameters on the command line. You should replicate how I ran it exactly.

Make sure you run the command:

$ 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

You can ensure that the arguments are supplied.

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...'

这篇关于如何在 Spyder 中使用 argv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆