先介绍几个概念:
回调函数:通俗讲是说把一个函数作为参数传给另一个函数,第一个函数称为回调函数
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