我没有得到这个程序的期望输出?
from sys import argv
script, first, second, third = argv
print ("The script is called:", script)
print ("Your first variable is:", first)
print ("Your second variable is:", second)
print ("Your third variable is:", third)
如何使用cmd传递这些参数?
你称之为
python program.py a1 b2 c3
它输出
The script is called: /home/sophia/program.py
Your first variable is: a1
Your second variable is: b2
Your third variable is: c3
sys.argv
包含字符串列表,每个字符串对应一个命令行参数。第一个始终是脚本的文件名;其他是可选参数,完全按照在shell中键入的顺序排序。
请注意,您提供的代码可以正常工作 只要 由于元组解包而准确传递三个参数。
请参阅文档 sys.argv中 并且还退房 argparse模块 文档,如果您要编写处理大量参数的程序。