livepython.net

177. Fix the Python Program That Never Closes Its File

Easywith

Overview

A file that isn't closed may not be saved yet. with closes it for you.

Problem Statement

The program in the editor should save a note in notes.txt and then print it back. Instead, it prints only:

Saved note:

The note was written, but the file was never closed, so the reader finds it empty. Fix the program with with open(...) as f:, so every file is closed automatically. The output should be Saved note: followed by the note.

The program uses input() with no prompt text: the tests type the input for you, and a prompt would end up in your output.

Input

One line: the note. Example:

Call Rocky at 5

Output

The output for that example:

Saved note: Call Rocky at 5

The tests try 3 different notes.

Learn this firstChapter 19 · Python with open(): Close Files and Handle Errors
solution.py
note = input()

f = open("notes.txt", "w")
f.write(note)

reader = open("notes.txt")
print("Saved note:", reader.read())
reader.close()

EXPECTED OUTPUT

1Saved note: Call Rocky at 5

YOUR OUTPUT

Press Run to see your output here. Lines are compared with the expected output.