想说明一些,因为这本书针对的是python2.x版本,而我一开始安装的就是python3.x版本,所以有部分代码无法运行,感谢网络上很多帮助和答案~
exercise 11 提问
作业代码:
print ("How old are you?"),
age = input()
print ("How tall are you ?",)
height = input()
print ("How much do you weigh?",)
weight = input() #在python 3.x中不再区分input()和raw_input()
print ("So, you're %r old, %r tall and %r heavy." % ( age, height, weight))
print ("So, you're %s old, %s tall and %s heavy." % ( age, height, weight))
运行结果:
How old are you?
22
How tall are you ?
160
How much do you weigh?
53
So, you're '22' old, '160' tall and '53' heavy.
So, you're 22 old, 160 tall and 53 heavy.
1.从最后两行输出的结果可知%s与%r输出结果的不同。
2. 在python 3.x中已经取消了raw_input()的说法,可以不需要再进行区分。
exercise12 提示别人
作业代码:
age = input ("How old are you ?")
height = input ("How tall are you ?")
weight = input ("How much do you weigh?")
print ("So, you're %s old, %s tall and %s heavy." % ( age, height, weight))
运行结果:
How old are you ?22
How tall are you ?160
How much do you weigh?53
So, you're 22 old, 160 tall and 53 heavy.
exercise13 参数、解包、变量
作业代码:
from sys import argv # 从sys模块/模组/库中导入argv函数(参数变量)
script, first, second, third = argv #unpack 解包 将每个参数赋予一个变量名
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 1 2 3 (可以替换为任意三样东西,script默认输出为文件名)
运行结果:
The script is called: ex13.py
Your first variable is : a
Your second variable is : b
Your third variable is: c
在运行中遇到了一些bug,主要需要知道argv的含义。另外,第一行script默认就输出的是文件名,是否可以有所不同?
加分习题2:再写两个脚本,其中一个接受更少的参数,另一个接受更多的参数,在参数解包时给它们取一些有意义的变量名。
尝试(在线追星)
代码:
from sys import argv
script , leader, dancer, rapper, vocal = argv
print (" Winner :" , script)
print (" the best leader:", leader)
print (" 百变精灵:", dancer)
print (" 宋画伯:",rapper)
print (" 小漂亮:",vocal)
运行结果:
Winner : ex13.1.py
the best leader: YOON
百变精灵: HOON
宋画伯: MINO
小漂亮: JINU
加分习题3 将 raw_input 和 argv 一起使用,让你的脚本从用户手上得到更多的输入。
1.在尝试中将四个变量都用input()表示出来,在输入变量结果后报错,原因仍然是没有足够的值进行解包,目前未解决该问题,以后再改吧~
2.argv 和 input() 有什么不同?
不同点在于用户输入的时机。如果参数是在用户执行命令时就要输入,那就是 argv,如果是在
脚本运行过程中需要用户输入,那就使用input()。
from sys import argv
script, leader, dancer, rapper, vocal = argv
leader = input ("who is the leader in Winner?:",)
dancer = input ("who is the dancer?:",)
vocal = input ("who is the vocal?:",)
rapper = input ("who is the rapper?:",)
print (" Winner :" , script)
print (" the best leader:", leader)
print (" 百变精灵:", dancer)
print (" 宋画伯:",rapper)
print (" 小漂亮:",vocal)
ERROR:
Traceback (most recent call last):
File "1.py", line 9, in <module>
leader, dancer, rapper, vocal = argv
ValueError: not enough values to unpack (expected 4, got 1)
———————————————看完14课后的修改——————————————————————————
from sys import argv
script, team_name = argv
leader = input ("who is the leader in %s ?:" % team_name)
dancer = input ("who is the dancer in %s?:" % team_name)
vocal = input ("who is the vocal in %s?:" % team_name)
rapper = input ("who is the rapper in %s?:" % team_name)
print (" the best leader:", leader)
print (" 百变精灵:", dancer)
print (" 宋画伯:",rapper)
print (" 小漂亮:",vocal)
运行结果:
PS C:\Users\Brenda\Desktop\Python学习路径资料\exercise> python 2.py winner
who is the leader in winner ?:YOON
who is the dancer in winner?:HOON
who is the vocal in winner?:JINU
who is the rapper in winner?:MINO
the best leader: YOON
百变精灵: HOON
宋画伯: MINO
小漂亮: JINU
遇到的问题!在使用格式化字符串的时候,leader = input ("who is the leader in %s ?:" % team_name),在提示语和%team_name之间不能有逗号comma断开。
exercise14 提示和传递
作业代码:
from sys import argv
script, user_name = argv
prompt = '> ' #将用户提示符统一设置为prompt
print ("Hi %s, I'm the %s script." % ( user_name, script))
print ("I'd like to ask you a few questions.")
print ("Do you like me %s?" %user_name)
likes = input (prompt)
print ("Where do you live %s?" %user_name)
lives = input(prompt)
print ("What kind of computer do you have?")
computer = input(prompt)
print ("""
Alright, so you said %r about liking me.
You live in %r. Not sure where that is.
And you have a %r computer. Nice.
""" % (likes, lives , computer))
运行结果:
PS C:\Users\Brenda\Desktop\Python学习路径资料\exercise> python 1.py lin
Hi lin, I'm the 1.py script.
I'd like to ask you a few questions.
Do you like me lin?
> yes
Where do you live lin?
> BJ
What kind of computer do you have?
> thinkpad
Alright, so you said 'yes' about liking me.
You live in 'BJ'. Not sure where that is.
And you have a 'thinkpad' computer. Nice.
ex14让我弄懂了ex13的加分题~
exercise15 读取文件
作业代码:
#第一种方式(argv)
from sys import argv
script, filename = argv
txt = open ( filename )
print ("Here's your file %r :" %filename )
print (txt.read())
#第二种方式(input)
print ("Type the filename again:")
file_again = input (">")
txt_again = open (file_again)
print (txt_again.read())
运行结果:
PS C:\Users\Brenda\Desktop\Python学习路径资料\exercise> python 1.py ex15_sample.txt
Here's your file 'ex15_sample.txt' :
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
Type the filename again:
>ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
主要需要熟悉argv(arguement variable)以及input()的使用方法。
网友评论