笨方法学python3 源码_笨办法学Python 练习13和14

原题:

1 from sys import argv

2

3 script, first, second, third = argv

4

5 print "The script is called:", script

6 print "Your first variable is:", first

7 print "Your second variable is:", second

8 print "Your third variable is:", third

这里要注意的是:argv ---相当于shell中的$参数,参数1, 参数2 = argv #之后参数的引用就和这个先后顺序有关了

在其给出的study drill的第三条:Combine raw_input with argv to make a script that gets more input from a user. 要求结合raw_input和argv创建一个脚本

在尝试用练习13 和之前的练习作素材来改程序失败的情况下,借鉴 笨办法学Python记录--习题12-14 主要是pydoc用法,raw_input,argv 并进行一定程度的改写,代码如下:

1 from sys import argv

2

3 script, user_name = argv

4

5 print "My name is %s, and you know I am the %s script." % (user_name, script)

6

7 like = raw_input("Do you like me?")

8 age = raw_input("How old are you?")

9

10 print """

11

12 OK, you said you %r me very much. You are %r years old.

13

14 """ %(like, age)

结果如下:(在powershell下执行)

L3Byb3h5L2h0dHAvaW1hZ2VzMjAxNS5jbmJsb2dzLmNvbS9ibG9nLzU4MDQwNS8yMDE2MDUvNTgwNDA1LTIwMTYwNTA4MDMxNzE1MDQ4LTk5MDY3OTYyOC5wbmc=.jpg

PS:

1. pydoc在Windows的用法,必须进入到python安装目录,执行python -m pydoc raw_input; (当然你可以把raw_input替换成其它模块,如open, os, )

下面是练习14的练习代码:

1 from sys import argv

2

3 script, user_name = argv

4 prompt = ‘> ‘

5

6 print "Hi %s, I‘m the %s script." % (user_name, script)

7 print "I‘d like to ask you a few questions."

8 print "Do you like me %s?" % user_name

9 likes = raw_input(prompt)

10

11 print "Where do you live %s?" % user_name

12 lives = raw_input(prompt)

13

14 print "What kind of computer do you have?"

15 computer = raw_input(prompt)

16

17 print"""

18 Alright, so you said %r about liking me.

19 You live in %r. Not sure where that is.

20 And you have a %r computer. Nice.

21 """ % (likes, lives, computer)

运行结果:

L3Byb3h5L2h0dHAvaW1hZ2VzMjAxNS5jbmJsb2dzLmNvbS9ibG9nLzU4MDQwNS8yMDE2MDUvNTgwNDA1LTIwMTYwNTA4MDM1NjI5NzUxLTEzMzYwNTkzMS5wbmc=.jpg

改写代码:

1 from sys import argv

2

3 script, user_name = argv

4

5 print "Hi, %s, I‘m the %s script." % (user_name, script)

6 print "I‘d like to ask you a few questions."

7

8 likes = raw_input("Do you like me %s?\n> " % user_name)

9 lives = raw_input("Where do you live %s?\n> " % user_name)

10 computer = raw_input("What kind of computer do you have?\n> ")

11

12 print """

13 Alright, so you said %r about liking me.

14 You live in %r. Not sure where that is.

15 And you have a %r computer. Nice.

16 """ % (likes, lives, computer)

显示结果是一样的,这里就不再贴图了。

时间: 05-05

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值