Accented comments read with a different encoding

Code review · Medium · §9.5, §9.7 [Pair coding with AI] Asking for file code

You asked an AI to count the comments in a comment file that contain the word café. This is the code it gave you. It runs without errors, but the count comes out as 0. comments.txt was saved in UTF-8. Fix one place.

Code to fix

count = 0
with open("comments.txt", encoding="latin-1") as f:
    for line in f:
        if "café" in line:
            count += 1
print("Comments that mention café:", count)

File comments.txt

Best café in town
Too noisy
I love this café!
It was okay

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.