标签:
原题:
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下执行)
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)
运行结果:
改写代码:
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)
显示结果是一样的,这里就不再贴图了。
标签:
原文地址:http://www.cnblogs.com/freescale/p/5469677.html