我自己写的: def average(*args): b = 0 if len(args) == 0: return b else: for i in args: b += i avg = b / len(args) return avg a=(1, 2, 3, 4, 5) print(average(a))
参考答案:
def average(*args): sum = 0 if len(args) == 0: return sum for item in args: sum += item avg = sum / len(args) return avg a=(1, 2, 3, 4, 5) print(average(a))
def average(*args): b = 0 if len(args) == 0: return b else: for i in args: b += i avg = b / len(args) return avg a=[1, 2, 3, 4, 5] print(average(*a)) <-----需要在前面加* 才會接受tuple/list用法