$python ex14.py Zed
Hi Zed,I'm the ex14.py script.
I'd like to ask you a few questions.
Do you like me Zed?
>yes
Where do you live Zed?
>America
What kind of computer do you have?
>Tandy
Alright, so you said 'yes' about liking me.
You live in 'America'. Not sure where that is.
And you have a 'Tandy' computer. Nice.
$
加分习题
1.查一下 Zork 和 Adventure 是两个怎样的游戏。 看看能不能下载到一版,然后玩玩看。
Zork I是电子游戏历史上最早的一款文字冒险游戏,是Colossal Cave Adventure的一个早期后继。
2.将 prompt 变量改成完全不同的内容再运行一遍。
ex14-2.py
from sys import argv
script, user_name = argv
prompt = "Pls input>"
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 = raw_input(prompt)
print "Where do you live %s?" % user_name
lives = raw_input(prompt)
print "What kind of computer do you have?"
computer = raw_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)
运行该脚本:
$python ex14-2.py Zed
Hi Zed,I'm the ex14-2.py script.
I'd like to ask you a few questions.
Do you like me Zed?
Pls input>yes
Where do you live Zed?
Pls input>us
What kind of computer do you have?
Pls input>Mac
Alright, so you said 'yes' about liking me.
You live in 'us'. Not sure where that is.
And you have a 'Mac' computer. Nice.
$
3.给你的脚本再添加一个参数,让你的程序用到这个参数。
ex14-3.py
from sys import argv
script, user_name, age = argv
prompt = '>'
print "Hi %s,I'm the %s script." %(user_name, script)
print "I'd like to ask you a few questions."
print "How old are you? %s." % age
print "Do you like me %s?" % user_name
likes = raw_input(prompt)
print "Where do you live %s?" % user_name
lives = raw_input(prompt)
print "What kind of computer do you have?"
computer = raw_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)
运行该脚本
$python ex14-3.py Zed 25
Hi Zed,I'm the ex14-3.py script.
I'd like to ask you a few questions.
How old are you? 25.
Do you like me Zed?
>yes
Where do you live Zed?
>us
What kind of computer do you have?
>Mac
Alright, so you said 'yes' about liking me.
You live in 'us'. Not sure where that is.
And you have a 'Mac' computer. Nice.
$
4.确认你弄懂了三个引号 """ 可以定义多行字符串,而 % 是字符串的格式化工具。
from sys import argv
script, name, age, hight = argv
prompt = 'lc>'
print "Hello %s ,It's a %s script." %(name, script)
print "Which fruit do you like? %s." % name
fruit = raw_input(prompt)
print "It's 7 o'clock,time to get up. Do you want to get up?"
get_up = raw_input(prompt)
print "When did you go to bed last night? %s." %name
time = raw_input(prompt)
print """
So your name is %s and you like %s
you are %s years old and you are %s tall
And you %s want to get up
And you went to bed at %s last night
""" % (name, fruit, age, hight, get_up, time)
运行该程序:
$python ex14-0.py cc 10 135cm
Hello cc ,It's a ex14-0.py script.
Which fruit do you like? cc.
lc>apple
It's 7 o'clock,time to get up. Do you want to get up?
lc>do not
When did you go to bed last night? cc.
lc>9 o'clock
So your name is cc and you like apple
you are 10 years old and you are 135cm tall
And you do not want to get up
And you went to bed at 9 o'clock last night
$
至此,笨方法学Python-ex14-提示和传递这一小节的学习结束了。