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

匿名 (未验证) 提交于 2019-12-03 03:02:02

问题:

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!

回答1:

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.



回答2:

Run it from the shell like this:

python script.py arg1 arg2 arg3 


回答3:

You could run it like this: python script.py first, second, third



回答4:

Try this, I tried it and it's working good

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) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!