Write a small linter that checks three PEP 8 rules on every line of a Python file:
- A line longer than 79 characters:
line N: too long (LENGTH > 79). - Spaces or tabs at the end of a line:
line N: trailing whitespace. - A tab at the start of a line:
line N: tab used for indentation.
Check every rule on every line, and report them in that order. At the end, print COUNT problem(s).
Input
The first line has n (1 ≤ n ≤ 200). The next n lines are the code (each at most 200 characters).
Output
One line for each problem found, in the order of the lines, then the count.
Examples
Example 1 · Input
3 def total(prices): return sum(prices) print(total([1, 2, 3]))
Expected output
line 1: trailing whitespace line 2: tab used for indentation 2 problem(s)
Example 2 · Input
1 result = first_value + second_value + third_value + fourth_value + fifth_value + sixth_value
Expected output
line 1: too long (92 > 79) 1 problem(s)
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
len(line) > 79 finds long lines.
Hint 2
line != line.rstrip() is True when the line ends with spaces or tabs.
Hint 3
line.startswith("\t") finds a tab at the start. Check all three rules on every line, in that order.
Solution
Show one correct solution (try it yourself first)
n = int(input())
problems = 0
for number in range(1, n + 1):
line = input()
if len(line) > 79:
problems += 1
print(f"line {number}: too long ({len(line)} > 79)")
if line != line.rstrip():
problems += 1
print(f"line {number}: trailing whitespace")
if line.startswith("\t"):
problems += 1
print(f"line {number}: tab used for indentation")
print(f"{problems} problem(s)")