1. 程式人生 > >笨辦法18命名、變量、代碼、函數

笨辦法18命名、變量、代碼、函數

重復 調用 vcd 表示 jsb font water actually avi

源代碼如下:

 1 #coding: utf-8
 2 # this one is like your scripts with argv
 3 def print_two(*args):
 4     arg1, arg2 = args
 5     print "arg1: %r, arg2: %r" % (arg1, arg2)
 6 
 7 # ok, that *args is actually pointless, we can just do this
 8 def print_two_again(arg3, arg4):
 9     print "arg3: %r, arg4: %r
" % (arg3, arg4) 10 11 # this just takes one argument 12 def print_one(arg5): 13 print "arg5: %r" % arg5 14 15 # this one takes no arguments 16 def print_none(): 17 print "I got nothin‘." 18 19 20 print_two("Zed1","Shaw1") 21 print_two_again("Zed2","Shaw2") 22 print_one("First!") 23 print_none()

運行結果:
技術分享

註:*args中,*表示將所有參數放在一個列表裏


加分習題?

函數定義是以 def 開始的嗎? (√)
函數名稱是以字符和下劃線 _ 組成的嗎? (× 字母、數字、下劃線,不能以數字開頭)
函數名稱是不是緊跟著括號 ( ? (√)
括號裏是否包含參數?多個參數是否以逗號隔開? (√)
參數名稱是否有重復?(不能使用重復的參數名)
緊跟著參數的是不是括號和冒號 ): ? (√)
緊跟著函數定義的代碼是否使用了 4 個空格的縮進 (indent)? (√)
函數結束的位置是否取消了縮進 (“dedent”)? (√)


‘運行函數(run)’、‘調用函數(call)’、和 ‘使用函數(use)’是同一個意思

笨辦法18命名、變量、代碼、函數