Line Counter
Write a program that writes a given number of lines to a text file, then reads that file back and counts its total number of lines.
What the problem means: this exercises the full write-then-read cycle — save some text to disk, then open it again and count how many lines it contains.
Approach: read how many lines there are and the lines themselves, write each one (with a newline) to a file using with open(...) as f:, then reopen it and count with readlines().
Input: First line: the number of lines n. Next n lines: the text content itself.
Output: One line: Total lines: <n>
3 Hello World Python
Total lines: 3
- 1 <= n <= 1000
Hint 1
with open("temp.txt", "w") as f: f.write(line + "\n") writes one line at a time, including the newline.
Hint 2
Reopen the file in "r" mode and use len(f.readlines()) to count how many lines it has.
Write every given line to a file (adding \n after each one so they land on separate lines), close it, then reopen it in read mode and count with len(f.readlines()) — readlines() returns one list entry per line, so its length is exactly the line count.