跟着笨方法学python个教程学习,边学变做习题边记录。
新手学python-笨方法学python-ex13-参数、解包和变量 - 学习记录
本小节学习内容:
在这节练习中,我们将讲到另外一种将变量传递给脚本的方法(所谓脚本,就是你写的.py程序)。
你已经知道,如果要运行ex13.py,只要在命令行运行python ex13.py就可以了。
这句命令中ex13.py部分就是所谓的“参数(argument)”,我们现在要做的就是写一个可以接受参数的脚本。
将下面的程序写下来,后面你将看到详细解释。
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
在第一行我们有一个“import”语句。这是你将python的功能引入你的脚本的方法。
Python不会一下子将它所有的功能给你,而是让你需要什么就调用什么。
这样可以让你的程序保持精简,而后面的程序员看到你的代码的时候,这些“import”可以作为提示,让他们明白你的代码用到了哪些功能。
argv是所谓的“参数变量(argument variable)”,是一个非常标准的变成术语。在其他的编程语言里你也可以看到它。
这个变量包含了你传递给Python的参数。通过后面的练习你将对它有更多的了解。
第3行讲argv“解包(unpack)”,与其将所有的参数放到同一个变量下面,我们将每个参数赋予一个变量名:script,first,second,以及third。
这样看上去有些奇怪,不过“解包”可能是最好的描述方式了。它的含义很简单:“把argv中的东西解包,将所有的参数一次赋予左边的变量名”。
等一下!“功能”还有另外一个名字
前面我们使用import让你的程序实现更多的功能,但实际上没人把import称为“功能”。我希望你可以在没接触到正式术语的时候就弄懂它的功能。
在继续下去之前,你需要知道它们真正的名称:模组(modules)。
从现在开始我们将把这些我们导入(import)进来的功能称作模组。你将看到类似这样的说法:“你需要把sys模组import进来。”也有人将它们称作“库(libraries)”,不过我们还是叫它们模组吧。
跟着课程学习:
输入不同的参数运行上面的脚本试试看结果:
$python ex13.py
Traceback (most recent call last):
File "ex13.py", line 3, in <module>
script, first, second, third = argv
ValueError: need more than 1 value to unpack
$
$python ex13.py
Traceback (most recent call last):
File "ex13.py", line 3, in <module>
script, first, second, third = argv
ValueError: need more than 1 value to unpack
$python ex13.py 1st
Traceback (most recent call last):
File "ex13.py", line 3, in <module>
script, first, second, third = argv
ValueError: need more than 2 values to unpack
$python ex13.py 1st 2nd
Traceback (most recent call last):
File "ex13.py", line 3, in <module>
script, first, second, third = argv
ValueError: need more than 3 values to unpack
$python ex13.py 1st 2nd 3rd
The script is called: ex13.py
Your first variable is: 1st
Your second variable is: 2nd
Your third variable is: 3rd
$python ex13.py one two three
The script is called: ex13.py
Your first variable is: one
Your second variable is: two
Your third variable is: three
$python ex13.py cheese apples bread
The script is called: ex13.py
Your first variable is: cheese
Your second variable is: apples
Your third variable is: bread
$
1.给你的脚本三个以下的参数。看看会得到什么错误信息。试着解释一下。
由上面的结果可以看到,输入三个一下的参数时,提示ValueError: need more than * value(s) to unpack,需要更多的值来解包。
2.再写两个脚本,其中一个接受更少的参数,另一个接受更多的参数,在参数解包时给它们取一些有意义的变量名。
ex13-2-1.py
from sys import argv
script, name, age, hight, weight = argv
print "What's your name?", name
print "How old are you?", age
print "How tall are you?", hight
print "How much are you weigh?", weight
运行该脚本
python ex13-2-1.py Vera 12 135cm 30kg
$python ex13-2-1.py Vera 12 135cm 30kg
What's your name? Vera
How old are you? 12
How tall are you? 135cm
How much are you weigh? 30kg
$
第二个脚本
ex13-2-2.py
from sys import argv
script, fruit, color = argv
print "What's this? \nThis is a %s.\nWhat's the color of it?\nIt's %s." %(fruit, color)
运行该脚本
python ex13-2-2.py apple red
python ex13-2-2.py pear yellow
python ex13-2-2.py orange orange
$python ex13-2-2.py apple red
What's this?
This is a apple.
What's the color of it?
It's red.
$python ex13-2-2.py pear yellow
What's this?
This is a pear.
What's the color of it?
It's yellow.
$python ex13-2-2.py orange orange
What's this?
This is a orange.
What's the color of it?
It's orange.
$
3.将 raw_input 和 argv 一起使用,让你的脚本从用户手上得到更多的输入。
ex13-3.py 这个不大会用,先这样分开用着吧
from sys import argv
script, first, second, third = argv
fourth = raw_input("Pls input the 4th variable: ")
fifth = raw_input("Pls input the 5th variable: ")
sixth = raw_input("Pls input the 6th variable: ")
print "The argv including these three variables: %s, %s and %s." %(first, second, third)
print "Thanks for inputting these three variables: %s, %s and %s." %(fourth, fifth, sixth)
执行该脚本
$python ex13-3.py 11 22 33
Pls input the 4th variable: 4
Pls input the 5th variable: 5
Pls input the 6th variable: 6
The argv including these three variables: 11, 22 and 33.
Thanks for inputting these three variables: 4, 5 and 6.
$
4.记住“模组(modules)”为你提供额外功能。多读几遍把这个词记住,因为我们后面还会用到它。
至此,笨方法学Python-ex13-参数、解包和变量这一小节的学习结束了。