Python学习8-参数 解包

上一篇 / 下一篇  2014-06-25 15:14:24 / 个人分类:笨办法学Python

$ex13.py 源码
#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)

from sys import argv #导入sys模组的argv功能

script, first, second, third, fouth, fifth = argv #解包并赋值

print (script)
print (first)
print (second)
print (third)
print (fouth)
print (fifth)

执行命令:
python .\ex13.py 1st 2nd 3rd 4th 5th

输出结果:
.\ex13.py
1st
2nd
3rd
4th
5th

使用参数时需要导入sys模组的argv功能
然后对argv进行解包,分别赋值给左侧的变量

$ex14.py 源码
from sys import argv
prompt = "\\:"
script, lll = argv

print ("Are you OK?")
status = input(prompt)

print ("So %r, your status is %r !" % (lll,status))

调用命令:
python .\ex14.py 22

输出结果:
Are you OK?
\:n
So '22', your status is 'n' !


TAG:

 

评分:0

我来说两句

Open Toolbar