answer12_3.py
total = 0
count = 0
with open("pop_shikoku.txt", encoding="utf-8") as file_obj:
for line in file_obj: # lineは"徳島県 685357"のような形式
count += 1
pref, pop = line.split()
total += int(pop)
total /= 10000 # 万人
print(f"合計{total:.1f}万人、平均{total/count:.1f}万人")
# 章末問題 12章【3】解答例
#
# 入力ファイルpop_shikoku.txtを開き(3行目)、for文を使ってその内容を
# 1行ずつ読みます(4行目〜)。各行は県名と人口がスペースで区切られた
# 形式なので、読んだ行を空白文字でsplitし(6行目)、人口を整数にして
# totalに足し込んでいきます(7行目)。
#
# 最後に人数を万人単位にし(8行目)、小数点以下1桁まで合計と平均を
# 出力します(9行目)。