加分习题3:将 raw_input 和 argv 一起使用,让你的脚本从用户手上得到更多的输入。
1 from sys import argv 2 3 script, first, second, third = argv #试了一下,这里的第一个参数script可以随便改成啥,不影响运行结果 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 9 ''' 10 11 raw_input("The script is called: "), script 12 raw_input("Your first variable is: "), first 13 raw_input("Your second variable is: "), second 14 raw_input("Your third variable is: "), third
不知道是不是这个意思,总觉得怪怪的,先这样后面再改!
运行结果如下,需要手动输入变量名:
==============下面重写!==============
1 from sys import argv 2 3 script, first, second, third = argv 4 prompt = ">" 5 6 print "please enter the script name:" 7 script = raw_input(prompt) 8 9 print "please enter the first variable:" 10 first = raw_input(prompt) 11 12 print "please enter the second name:" 13 second = raw_input(prompt) 14 15 print "please enter the third name:" 16 third = raw_input(prompt) 17 18 print """ 19 The script is called:%r 20 The first variable is:%r 21 The second variable is:%r 22 The third variable is:%r 23 """ % (script, first, second, third)
运行结果:
ps:但是怎么才能把引号去掉呢。。 埃玛以后再说吧!
来解答! 重新翻看了之前的第6课,只要把%r换成%s就没有引号啦!!
20170914 按照22课练习来复习
argv 和 raw_input()的区别:要求用户输入的位置不同。在命令行输入参数用argv.,在脚本执行的过程中输入参数用raw_input()