ある中学校3年A組,B組,C組で数学のテストを行った。 A組は12人,B組は8人,C組は10人であり,各クラスの生徒の得点は, クラス毎にリストa, b, cに収められている(test_score.py参照)。 クラス毎に平均値(受験者の合計得点を受験者数で割ったもの)を求めなさい。
# coding: shift_jis a = [70, 50, 64, 45, 64, 42, 60, 36, 56, 20, 54, 16] b = [46, 66, 60, "欠席", 66, 62, 58, 28] c = [70, 75, 82, 46, 61, "欠席", 68, 73, 76, 65] def mean(score): N = len(score) #リストscoreの要素数 s = 0 #合計を保存するための変数 n = 0 #受験者数 i = 0 while i < N: if not(score[i] == "欠席"): s = s + score[i] n = n + 1 i = i + 1 return s / n print("Aクラスの平均値:", mean(a)) print("Bクラスの平均値:", mean(b)) print("Cクラスの平均値:", mean(c))