笨辦法13參數、解包、變量_草稿
阿新 • • 發佈:2017-10-11
ont prompt mar all 是不是 nbsp ext gravity 重寫
加分習題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:", third9 ‘‘‘ 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 importargv 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 15print "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()
笨辦法13參數、解包、變量_草稿