笨方法学python 习题13

习题13
python:3.9


```python3.9
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)

打完代码,输出的结果是

python ex13.py first 2nd 3rd
The script is called: ex13.py
Your first variable is: first
Your second variable is: 2nd
Your third variable is: 3rd


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


python ex13.py Zed A. Shaw
The script is called: ex13.py
Your first variable is: Zed
Your second variable is: A.
Your third variable is: Shaw


python ex13.py stuff I like
The script is called: ex13.py
Your first variable is: stuff
Your second variable is: I
Your third variable is: like


python ex13.py anything 6 7
The script is called: ex13.py
Your first variable is: anything
Your second variable is: 6
Your third variable is: 7
如果你没有运行对,你将看到如下错误:python ex13.py first 2ndTraceback (most recent call last):File "ex/ex13.py", line 3, in <module>script, first, second, third = argvValueError: need more than 3 values to unpack当你运行脚本时提供的参数的个数不对的时候,你就会看到上述错误信息(这次我只用了 first 2nd两个参数)。“need more than 3 values to unpack”这个错误信息告诉你参数数量不足。
 **加分习题**
 1.给你的脚本三个以下的参数。看看会得到什么错误信息。试着解释一下。
 *同上*
  2.再写两个脚本,其中一个接受更少的参数,另一个接受更多的参数,在参数解包时给它们取一些有意义的变量名。
  这个自己随便写就ok
  3.将raw_input和 argv一起使用,让你的脚本从用户手上得到更多的输入。
  

```python3.9
sex = input("What is you gender ?")
thing= input("What do you do?")
attitude =input ("whether you like what you are doing?")
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)
print ("So,you're %r,and you %r %r."%(sex,attitude,thing))

运行结果为

PS C:\Users\78523\mybuff> python ex13.py 0 2 5
What is you gender ?men
What do you do?writing code
whether you like what you are doing?like
The script is called: ex13.py
Your first variable is0
Your second variable is : 2
Your third variable is5
So,you're 'men',and you 'like' 'writing code '.

4.记住“模组(modules)”为你提供额外功能。多读几遍把这个词记住,因为我们后面还会用到它。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值