Day3
- 習題 13: 參數、解包、變量
from sys import argv script, first, second, third = argv print("The script is called:",script) print("Your first variable is:",first) print("Your second variable is:",second) print("Your third variable is:",third) #運行power shell #cd E:\py #似乎是只能進到一級文件夾 #python 13.py first 2nd 3rd
powershell 運行結果
第 3 行將 argv “解包(unpack)”,與其將所有參數放到同一個變量下面,我們將
每個參數賦予一個變量名: script, first, second, 以及 third。這也許看上
去有些奇怪, 不過”解包”可能是最好的描述方式了。它的含義很簡單:“把 argv
中的東西解包,將所有的參數依次賦予左邊的變量名”。
-
習題 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 = input(prompt) 10 11 print("Where do you live %s?" % user_name) 12 lives = input(prompt) 13 14 print("What kind computer do you have?") 15 computer = input(prompt) 16 17 print(""" 18 Alright, so you said %s about liking me. 19 You live in %s. Not sure where what is. 20 And you have a %s computer.Nice. 21 """ % (likes, lives, computer))
注意:
print("%r %s" % (12, 25))
多個格式化字符,記得要在print 的括號里,而且還要一個括號括起來
-
習題 15: 讀取文件
1 from sys import argv 2 3 script, filename = argv 4 5 txt = open(filename) 6 7 print("Here's your file %r:" % filename) 8 print(txt.read()) 9 10 print("Type the filename again:") 11 file_again = input("> ") 12 txt_again = open(file_again) 13 14 print(txt_again.read())
-
習題 16: 讀寫文件
1 from sys import argv 2 3 script, filename = argv 4 5 print("We are going to erase %r." % filename) 6 print("If you don't want that, hit CTRL-C(^C).") 7 print("If you do want that, hit RETURN.") 8 9 input("?") 10 11 print("Opening the file...") 12 target = open(filename, 'w')#以寫模式打開文件,其實會新建一個文件,若原來有,則會被這個覆蓋 13 14 print("Truncating the file. Goodbye!") 15 target.truncate() 16 17 print("Now I'm going to ask you for three lines.") 18 19 line1 = input("line 1:") 20 line2 = input("line 2: ") 21 line3 = input("line 3: ") 22 23 print("I'm going to write these to the file.") 24 25 target.write(line1) 26 target.write("\n") 27 target.write(line2) 28 target.write("\n") 29 target.write(line3) 30 target.write("\n")
#用一行寫出來: 31 #target.write(line1+"\n"+line2+'\n'+line3)
32 print("And finally, we close it.") 33 target.close()
-
習題 17: 更多文件操作
1 from sys import argv 2 from os.path import exists 3 4 script, from_file, to_file = argv 5 6 print("Copying from %s to %s" % (from_file, to_file)) 7 8 #we could do these two on one line too, how? 9 input1 = open(from_file) 10 indata = input1.read() 11 12 print("Does the output file exist? %r" % exists(to_file)) 13 print("Ready,hit RETURN to continue, CTRL-C to abort.") 14 input() 15 16 output = open(to_file,'w') 17 output.write(indata) 18 19 print("Alright, all done.") 20 21 output.close() 22 input1.close()
第一次報錯因為17行,open的時候沒有指定以寫模式打開,記住,open(file,'w') 要指定打開模式
-
習題 18: 命名、變量、代碼、函數
1 #this one is like your script with argv 2 def print_two(*args): 3 arg1, arg2 = args 4 print("arg1: %r, arg2: %r" % (arg1, arg2)) 5 6 #ok, that *args is actually pointless, we can just do this 7 def print_two_again(arg1, arg2): 8 print("arg1: %r, arg2: %r" % (arg1,arg2)) 9 10 #this just takes one argument 11 def print_one(arg1): 12 print("arg1: %r" % arg1) 13 14 #this takes no arguments 15 def print_none(): 16 print("I got nothin'.") 17 18 19 print_two("Zed","Shaw") 20 print_two_again("MI","YO") 21 print_one("only one") 22 print_none() 23 24 def print_three(a,b,c): 25 print("%s %r %r" % (a, b, c)) 26 print_three("a",'b','c')
arg1: 'Zed', arg2: 'Shaw'
arg1: 'MI', arg2: 'YO'
arg1: 'only one'
I got nothin'.
a 'b' 'c'
1. 首先我們告訴 Python 創建一個函數,我們使用到的命令是 def ,也就是“定義
(define)”的意思。
2. 緊接着 def 的是函數的名稱。本例中它的名稱是 “print_two”,但名字可以隨便取,
就叫 “peanuts” 也沒關系。但最好函數的名稱能夠體現出函數的功能來。
3. 然后我們告訴函數我們需要 *args (asterisk args),這和腳本的 argv 非常相似,
參數必須放在圓括號 () 中才能正常工作。
4. 接着我們用冒號 : 結束本行,然后開始下一行縮進。
5. 冒號以下,使用 4 個空格縮進的行都是屬於 print_two 這個函數的內容。 其中
第一行的作用是將參數解包,這和腳本參數解包的原理差不多。
6. 為了演示它的工作原理,我們把解包后的每個參數都打印出來,這和我們在之前腳
本練習中所作的類似。
加分題:
1. 函數定義是以 def 開始的嗎? yes
2. 函數名稱是以字符和下划線 _ 組成的嗎? yes
3. 函數名稱是不是緊跟着括號 ( ? yes
4. 括號里是否包含參數?多個參數是否以逗號隔開? yes
5. 參數名稱是否有重復?(不能使用重復的參數名) no
6. 緊跟着參數的是不是括號和冒號 ): ? yes
7. 緊跟着函數定義的代碼是否使用了 4 個空格的縮進 (indent)? yes
8. 函數結束的位置是否取消了縮進 (“dedent”)? yes