笨办法学python2.0 习题11-25

习题11

#在 python3 中对 input 语句进行了简化,由 python2 中的两条语句( raw_input() input() )简化为了一条语句( input )
#Note: 注意到我在每行 print 后面加了个逗号 (comma) , 了吧?这样的话 print 就不会输出新行符而结束这一行跑到下一行去了。
# 上条适用于PY2 在PY3中默认添加一个/N 所以要不输出新行需要加END
#input()函数接收一个标准输入数据,返回为string类型。
print("How old are you?", end=" ")
age = input()
print("How tall are you?",end=" ")
height = input()
print("How much do you weight?",end=" ")
weight = input()
print("So you're %r old, %r tall and %r heavy. "%(age,height,weight))
额外内容
Python3 VS. Python2
在 python3 中对 input 语句进行了简化,由 python2 中的两条语句( raw_input() input() )简化为了一条语句( input )。

在 python2中:
raw_input() 把所有输入都作为字符串看待。而 input() 则只能接受数字,可以执行数字有关的 python 表达式。
例如输入加法 2 + 3 raw_input 返回字符串 '2 + 3' 而 		input() 返回整数 5 。

在 python3 中:
两种 input 合并为一个 input() 函数接受任意输入,但是默认都作为字符串处理。

习题12

#pydoc 这个我在powershell 和 CMD里 执行不了 是没装mod的原因吗 =-= 但是HELP命令能用 先凑合后期解决
age = input("How old are you? ")
height = input("How tall are you? ")
weight = input("How much do you weight? ")

print("So you're %r old, %r tall and %r heavy. "%(age,height,weight))

习题13

这个题有点绕 关键点在于不是让你ctrl+shift+f10去运行 而是在操作台 如powershell,cmd上呈现结果
如果你对着代码一直ctrl+shift+f10 会一直是报错 =-= 浪费我20分钟

from sys import argv

script,frist,second,third = argv

print("The script is called:",script)
print("Your first variable is:",frist)
print("Your second variable is:",second)
print("Your thrid variable is:",third)

呈现结果

PS E:\pythonlearning\venv> python ex13.py 1 2 3 4       
  File "E:\pythonlearning\venv\ex13.py", line 3, in <module>
    script,frist,second,third = argv
The script is called: ex13.py
Your first variable is: 1
Your second variable is: 2
Your thrid variable is: 3
PS E:\pythonlearning\venv> ^C
PS E:\pythonlearning\venv> python ex13.py cheese apple bread
The script is called: ex13.py
Your first variable is: cheese
Your second variable is: apple
Your thrid variable is: bread

13.1 使用不同数量的参数会怎样?

PS F:\python\笨办法学python3> python ex13.py 两个 参数
Traceback (most recent call last):
  File "ex13.py", line 3, in <module>
    script, first, second, third = argv
ValueError: not enough values to unpack (expected 4, got 3)

当我使用两个参数的时候出错了
首先程序指出我们脚本第 3 行写的是 script, first, second, third = argv 这是刚刚解包的位置。
python 提示 ValueError 值出错了,具体错误是 not enough values to unpack (expected 4, got 3) 没有足够的值解包。

通对观察前面正确的截图,对比第 3 行的代码可以看出一些问题:

argv 解包的第一个值,对应变量 script 永远都是我们运行的脚本的名字。
命令行中最后三个参数分别对应了 first, second, third 三个变量。
错误的问题就在于 third 没有办法取得参数了,因为我们只输入了两个参数。
而当我们多一个参数时会这样

PS F:\python\笨办法学python3> python ex13.py 第一 第二 第三 参数
Traceback (most recent call last):
  File "ex13.py", line 3, in <module>
    script, first, second, third = argv
ValueError: too many values to unpack (expected 4)

和上面的情况相反,这次错误信息告诉我们参数太多了。
可以看出 python 的程序非常严谨,多一个不行少一个也不行。而且错误提示太厉害了是不是

13.2 双参数和四参数版本
from sys import argv

双参数
script, first, second = argv    # 删掉一个就好了

print("The script is called:", script)
print("Your first variable is:", first)
print("Your second variable is:", second)
四参数
from sys import argv

script, first, second, third, fourth = 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("Your fourth variable is:", fourth) 

附加练习3:把 input 和 argv 结合起来创建一个脚本,从用户那里获取更多 input 。别想得太难,就用 argv 来获取一些东西,再用 input 从用户那里获取一些东西。

from sys import argv
argv = input().split()

script,frist,second,third = argv

print("The script is called:",script)
print("Your first variable is:",frist)
print("Your second variable is:",second)
print("Your thrid variable is:",third)
PS E:\pythonlearning\venv> python ex13.py
1 2 3
Traceback (most recent call last
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值