先介紹幾個概念:ide
回調函數:通俗講是說把一個函數做爲參數傳給另外一個函數,第一個函數稱爲回調函數函數
def computer(a, b, func): return func(a, b) def max(a, b): return [a, b][a < b] # 意思是若是a<b,則返回a,不然返回b def min(a, b): return [a, b][a > b] def sum(a, b): return str(int(a) + int(b)) if __name__ == "__main__": a = input("請輸入整數a:") b = input("請輸入整數b:") res = computer(a, b, max) print("Max of " + a + " and " + b + " is " + res) res = computer(a, b, min) print("Min of " + a + " and " + b + " is " + res) res = computer(a, b, sum) print("Sum of " + a + " and " + b + " is " + res) 輸出結果: 請輸入整數a:5 請輸入整數b:6 Max of 5 and 6 is 6 Min of 5 and 6 is 5 Sum of 5 and 6 is 11