Reading a file outside the with block
Code review · Medium · §9.3, §9.4 Syntax that never forgets, and the contract of modes
This code tries to count the posts in likes.txt that have 100 or more likes. When you run it, it stops with an error. Read the last line of the error message and fix the code.
Code to fix
popular = 0
with open("likes.txt") as f:
print("Reading likes.txt.")
for line in f:
if int(line) >= 100:
popular += 1
print("100 or more:", popular)File likes.txt
120
80
200
45Solve 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.