新手学python-笨方法学python-ex14-提示和传递

跟着个教程学习,边学边做习题边记录。

新手学python-笨方法学python-ex14-提示和传递 - 学习记录
上一小节的加分题3想不出来argv和raw_input怎么结合使用,就分开使用了。刚好这一小节学到,真好。

本小节学习内容:

让我们使用argv和raw_input一起来向用户提一些特别的问题。下一节习题你会学习如何读写文件,这节练习是下节的基础。
这道练习题里我们将略微不同的方法使用raw_input,让它打出一个简单的>作为提示符。这和一些游戏中的方式类似,例如Zork或者Adventure这两款游戏。

from sys import argv

script, user_name = argv
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 = 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)

 

我们将用户提示符设置为变量 prompt,这样我们就不需要在每次用到raw_input的时候重复输入提示用户的字符了。而且如果你要将提示符修改成别的字串,你只需要修改一个位置就可以了。

非常顺手吧。

执行这个脚本,我们将看到结果:


$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


$

 

 

至此,笨方法学-ex14-提示和传递这一小节的学习结束了。

Friday, October 30, 2015 | Python

文章评论

No comments posted yet.

发表评论

Please add 2 and 6 and type the answer here: