A save function that writes the newline first
Code review · Medium · §9.6 The math helper remembers
This code tries to save daily view counts to a file and read them back later. When you run it, it stops with an error when it reads the file back.
Fix one place so that the file has one number per line, with a newline at the end of every line. Grading also checks the content of the saved file.
Code to fix
def save_views(views, filename):
with open(filename, "w", encoding="utf-8") as f:
for n in views:
f.write(f"\n{n}")
def load_views(filename):
views = []
with open(filename, encoding="utf-8") as f:
for line in f:
views.append(int(line))
return views
save_views([320, 410, 385], "views.txt")
history = load_views("views.txt")
print(history)
print("Most views in a day:", max(history))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.