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
。我想了解为什么我收到错误以及如何解决它。谢谢!
答案 0 :(得分:3)
从shell运行它:
python script.py arg1 arg2 arg3
答案 1 :(得分:3)
argv
变量包含命令行参数。在你的代码中,你期望4个参数,但只得到1(第一个参数总是脚本名称)。您可以在pycharm
中配置参数。转到Run
- > Edit Configurations
。然后创建一个新的python配置。在那里你可以指定Script parameters
字段。或者您可以从命令行运行脚本,如dnit13所述。
答案 2 :(得分:0)
你可以这样运行:python script.py first,second,third
答案 3 :(得分:0)
我认为您正在运行以下命令:
python script.py
您正在编写一个需要4个输入的程序,并且只给出一个。这就是为什么您收到一个错误。您可以使用以下命令:
python script.py Hello How Are
答案 4 :(得分:-2)
试试这个,我试过了,它运作良好
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)