list1 = [2, 3, 9, 7, 3, 4] def avg_func(list1): sum1 = 0 i = 0 while i < len(list1): sum1 += list1[i] i += 1 avg = sum1 / len(list1) return avg sum2 = avg_func(list1) print(sum2, type(sum2))
#运行结果
#第二题
list2 = [1, 2, 3, 4, 5] def avg_func(list2): sum1 = 0 i = 0 list_max = [] list_min = [] while i < len(list2): sum1 += list2[i] i += 1 avg = sum1 / len(list2) print(avg) aa = 0 while aa < len(list2): if int(list2[aa]) > avg: list_max.append(list2[aa]) else: list_min.append(list2[aa]) aa += 1 # return list_min, list_max print(list_min, list_max) print(avg) avg_func(list2)
#运行结果