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())

 

转载于:https://www.cnblogs.com/shadowyuriya/p/9966740.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值