笨办法学python11-15

想说明一些,因为这本书针对的是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()的使用方法。

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 204,293评论 6 478
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 85,604评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 150,958评论 0 337
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,729评论 1 277
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,719评论 5 366
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,630评论 1 281
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,000评论 3 397
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,665评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,909评论 1 299
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,646评论 2 321
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,726评论 1 330
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,400评论 4 321
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,986评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,959评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,197评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 44,996评论 2 349
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,481评论 2 342

推荐阅读更多精彩内容