python习题13

1 from sys import argv
2 # read the WYSS section for how to run this
3 script,first,second,third = argv
4 
5 print("The script is called:", script)
6 print("Your first variable is:", first)
7 print("Your second variable is:", second)
8 print("Your third variable is:", third) 

*给这个脚本少于三个参数,得到的出错消息如图

试着解释:到程序第三行的时候,依次把参数赋值给变量,但是到了third的时候,少了一个,无法继续。

 

再写两个脚本,其中一个接收更少的参数,另一个接收更多的参数,在参数解包的时候给它们取一些有意义的变量名。

1 from sys import argv
2 script,name,age = argv
3 
4 print("This script is called",script)
5 print("My name is",name)
6 print("My age is",age)
1 from sys import argv
2 script,name,age,height,weight = argv
3 
4 print("This script is called",script)
5 print("My name is",name)
6 print("My age is",age)
7 print("My height is",height)
8 print("My weight is",weight)

 

将input和argv一起用,让脚本从用户那边得到更多的输入。(不要想多了,只是用argv得到一些东西,用input从用户那里得到另外一些东西。

1 from sys import argv
2 script,name = argv
3 #后果是输入的内容赋值给了变量....
4 script = input("what's the script called?")
5 name = input("what's your name?")

 

 1 from sys import argv
 2 # read the WYSS section for how to run this
 3 script,first,second,third = argv
 4 
 5 print("The script is called:", script)
 6 print("Your first variable is:", first)
 7 print("Your second variable is:", second)
 8 print("Your third variable is:", third) 
 9 
10 name = input("My name is ")
11 age  = int(input("My age is "))
12 
13 print(f"My name is {name},and age is {age} .")

 

* import语句是把python特性引入脚本的一种方法,要什么就调用什么

*argv :参数变量,保存着你运行python脚本的时候传给脚本的参数。

*对argv解包:就是把所有参数依次赋值给argv左边的变量

 

**导入的这些特性应该被称为模块,也有人将这个叫做库。

 

*argv和input()的区别:在于用户输入的时机,一个是命令行输入,一个是运行的时候输入。

*命令行的参数是字符串,应该先用int()转换为整数。类似于int(input())

 

posted @ 2018-11-15 23:04  yuriya  阅读(380)  评论(0编辑  收藏  举报