繁体   English   中英

ValueError:没有足够的值来解包(预期为 4,得到 1)

[英]ValueError: not enough values to unpack (expected 4, got 1)

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)

错误在script, first, second, third = argv 我想了解为什么我会收到错误以及如何修复它。 谢谢!

像这样从 shell 运行它:

python script.py arg1 arg2 arg3

argv变量包含命令行参数。 在您的代码中,您期望有 4 个参数,但只有 1 个(第一个参数始终为脚本名称)。 您可以在pycharm配置参数。 转到Run -> Edit Configurations 然后创建一个新的python配置。 在那里你可以指定Script parameters字段。 或者您可以从命令行运行脚本,如 dnit13 所述。

你可以像这样运行它:python script.py first, second,third

我认为您正在运行以下命令:

python script.py

您正在编写一个需要 4 个输入的程序,而您只提供了一个。 这就是您收到错误的原因。 您可以使用以下命令:

python script.py Hello How Are

试试这个,我试了一下,它运作良好

from sys import argv

script, first, second, third = argv,"Anything Here 1","Anything Here 2","Anything Here 3"

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM