Iterators and generators · 9 / 15 Exercise Medium

Lines without comments

Objective

Create a Python program with a generator that filters the lines of a text, processing them one at a time.

Instructions

  • Read an integer n and then n lines of text, which are stored in a list.
  • Define def without_comments(lines), which goes through the lines and yields only those that do not start with #.
  • Show each yielded line with its sequence number starting at 1, with the format N: line (use enumerate(..., 1)).
  • At the end show Total: K, with the number of lines that were not comments.
Input
4
# configuración
x = 1
# otra nota
y = 2
Output
1: x = 1
2: y = 2
Total: 2

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise