livepython.net

174. Python Program to Read a File Line by Line

EasyRead

Overview

A for loop reads a file one line at a time. strip() cleans each line.

Problem Statement

Read a whole number n, then n names, one per line. Write the names into a file called names.txt, one per line, and close it. Then open names.txt again and loop over its lines, printing each name with its line number, a dot and a space in front: 1. Sunny.

Your program runs in the browser, so the file lives only in this tab's memory. Create it with "w" before you read it.

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

Input

The number of names, then the names. Example:

3
Sunny
Rocky
Lucky

Output

The output for that example:

  1. Sunny\
  2. Rocky\
  3. Lucky

The tests try 3 different inputs.

Learn this firstChapter 19 · Python Read File: read(), readline() and Lines
solution.py
# write your code here

EXPECTED OUTPUT

11. Sunny
22. Rocky
33. Lucky

YOUR OUTPUT

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