Passing a number straight to write()

Code review · Basic · §9.2 Writing to a file

This code tries to write view counts to views.txt, one per line, and read the file back to check it. When you run it, it stops with an error. Fix one line so that the view counts print one per line. Leave the views line at the top as it is.

Code to fix

views = [120, 95, 210]
f = open("views.txt", "w")
for v in views:
    f.write(v)
    f.write("\n")
f.close()

f2 = open("views.txt", "r")
print(f2.read())
f2.close()

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.