script, first, second, third = argv

from sys import argv                           # 从Python的标准库之一sys模块库中引入argv模块到自己的脚本中
# read the WYSS section for how to run this
script, first, second, third = argv            # 解包argv,并依次赋值给左边的变量    

print("The script is called:", script)
print("You first variables is:", first)
print("Your second variables is:", second)
print("Your third variables is:", third)

# 运行命令
# Python ex13.py 1st 2 03
# 相当于依次赋值了4个值给argv左边

 

在Powershell中运行报错

python ex13.py
Traceback (most recent call last):
  File "E:\python3\di1gexiangmu20231219\ex13.py", line 14, in <module>
    script, first, second, third = argv
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ValueError: not enough values to unpack (expected 4, got 1)

运行时同时按变量个数对应赋值

python ex13.py 1st 2 03

正确运行,结果为

The script is called: ex13.py
You first variables is: 1st
Your second variables is: 2
Your third variables is: 03

 

posted @ 2023-12-27 15:36  中年二班  阅读(29)  评论(0编辑  收藏  举报