可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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)