Counting survey answers

Write code · Medium · §4.2, §9.3 Reading a file

answers.txt holds survey answers, one per line. Count how many times each answer appears and store the counts in the dictionary counts. Each key is an answer with its newline removed, and each value is the number of times it appears. Do not count blank lines.

Starter code

counts = {}
f = open("answers.txt", "r")
# Write your code here

f.close()
print(counts)

File answers.txt

yes
no
yes
maybe
yes

Solve this problem in your browser

Python runs in your browser and checks your answer right away. Hints and the answer are on the problem page.