Python ValueError:没有足够的值来解包(预期为 4,得到 1)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36093126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ValueError: not enough values to unpack (expected 4, got 1)
提问by sed174
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("The third variable is: ", third)
The error is at script, first, second, third = argv
. I would like to understand why I am getting the error and how to fix it. Thank you!
错误在script, first, second, third = argv
。我想了解为什么我会收到错误以及如何修复它。谢谢!
采纳答案by kvorobiev
argv
variable contains command line arguments. In your code you expected 4 arguments, but got only 1 (first argument always script name). You could configure arguments in pycharm
. Go to Run
-> Edit Configurations
. Then create a new python configuration. And there you could specify Script parameters
field. Or you could run your script from command line as mentioned by dnit13.
argv
变量包含命令行参数。在您的代码中,您期望有 4 个参数,但只有 1 个(第一个参数始终为脚本名称)。您可以在pycharm
. 转到Run
-> Edit Configurations
。然后创建一个新的python配置。在那里你可以指定Script parameters
字段。或者您可以从命令行运行脚本,如 dnit13 所述。
回答by dnit13
Run it from the shell like this:
像这样从 shell 运行它:
python script.py arg1 arg2 arg3
回答by user3900428
You could run it like this: python script.py first, second, third
你可以像这样运行它:python script.py first, second,third
回答by Ashutosh Singla
I think you are running the following command:
我认为您正在运行以下命令:
python script.py
You are writing a program which is aksing for 4 inputs and you are giving onle one. That's why you are receiving an error. You can use the below command:
您正在编写一个需要 4 个输入的程序,而您只提供了一个。这就是您收到错误的原因。您可以使用以下命令:
python script.py Hello How Are