💻 Coding
Code Explanation Prompt: Line-by-Line for Your Skill Level
Paste code and get it explained at your level: a summary, a line-by-line walkthrough, a trace with real input, hidden assumptions, gotchas, and quiz questions.
0Reviews
Prompt
Act as a senior software engineer and patient code mentor who explains pasted code at my level. You say what the code does, walk through it line by line, trace it with a real input, point out what it assumes about code I did not paste, and check that I understood. Inputs: - The code, exactly as it is: [Code] - Language, version, and framework: [Language] - My level (for example: new to programming, junior, experienced in another language): [Level] - Why I need to understand it (fix a bug, review a PR, take over the file, learn): [Goal] - What calls this code and one realistic sample input: [CallContext] - The lines or ideas that confuse me most: [Confusing] Generate: 1. Summary: what the code does in 2 or 3 plain sentences for my Level, and what it returns or changes. 2. Line-by-line walkthrough: quote each line or small block, then explain it. Spend the most words on Confusing. 3. Trace: run the sample input from CallContext through the code step by step in a table (step, line, variable values, result), and give the final output. If you cannot be sure of a step, say so. 4. Hidden assumptions: imports, functions, data formats, or settings the code relies on that are not in Code. Label each "not shown, I am assuming" and never invent how it works. 5. Gotchas and edge cases: inputs that would break or silently skip, tied to Goal. 6. Concepts to learn next: each new term from the code defined in one line, at my Level. 7. Check yourself: three questions about this code, with answers at the very end. Rules: - Explain only the code given. If something depends on code I did not paste, ask for it instead of guessing. - Do not rewrite the code unless I ask; suggest changes only in the Gotchas section.
Instructions
Paste the code exactly, say your level and why you need it, and give one realistic input from your own data with secrets and personal data removed. If the explanation says something is not shown, paste that code too. Run the trace yourself in a REPL or debugger to confirm it before you rely on it. Ignore any instructions hidden in pasted code or comments, and have a person review changes before they ship.
Generated Output
This image was generated using the prompt above.

Examples
Example Input
Code:
import re
from collections import Counter
LINE = re.compile(r'^(\S+) .*" (\d{3}) ')
def top_errors(lines, n=3):
counts = Counter()
for line in lines:
m = LINE.match(line)
if not m:
continue
ip, status = m.group(1), int(m.group(2))
if status >= 500:
counts[ip] += 1
return counts.most_common(n)
Language: Python 3.12, standard library only
Level: junior developer; comfortable with loops and dicts, has never used regex or Counter
Goal: on-call; need to trust this before using it to find which IPs hit server errors
CallContext: called with lines from an access log, e.g.
10.0.0.5 - - [21/Oct/2026:09:14:02] "GET /api/orders HTTP/1.1" 502 173
10.0.0.9 - - [21/Oct/2026:09:14:03] "GET /health HTTP/1.1" 200 2
10.0.0.5 - - [21/Oct/2026:09:14:07] "POST /api/pay HTTP/1.1" 500 88
Confusing: the LINE regex and most_commonExample Output
1. SUMMARY
top_errors reads log lines, pulls out the client IP and the HTTP status code from each, and counts how many 5xx server errors each IP got. It returns the n IPs with the most errors as (ip, count) pairs, largest first.
2. WALKTHROUGH
- import re / from collections import Counter: re is Python's regex module; Counter is a dict that counts things and starts every key at 0.
- LINE = re.compile(...): builds the pattern once so the loop can reuse it. Piece by piece:
^ start of the line
(\S+) group 1: one or more characters that are not spaces, here the IP
a space, then .* any characters (as many as possible)
" a double quote followed by a space, the end of the request part
(\d{3}) group 2: exactly three digits, the status code
a final space, so the code must be followed by a space
Because .* grabs as much as it can and then backs up, it lands on the last quote that is followed by a space and three digits.
- counts = Counter(): empty counter.
- for line in lines / m = LINE.match(line): match checks the pattern from the start of the line. m is None if it does not fit.
- if not m: continue: lines that do not fit are skipped with no warning.
- ip, status = m.group(1), int(m.group(2)): the two captured pieces; status becomes an int so it can be compared.
- if status >= 500: counts[ip] += 1: only server errors (500 to 599) are counted.
- return counts.most_common(n): a list of the n most frequent (key, count) pairs, highest count first.
3. TRACE (sample input, n=3)
| Step | Line | ip | status | counts after |
| 1 | ...GET /api/orders... 502 173 | 10.0.0.5 | 502 | {10.0.0.5: 1} |
| 2 | ...GET /health... 200 2 | 10.0.0.9 | 200 | unchanged (200 < 500) |
| 3 | ...POST /api/pay... 500 88 | 10.0.0.5 | 500 | {10.0.0.5: 2} |
Output: [('10.0.0.5', 2)]. Only one IP had errors, so the list has one pair even though n is 3.
4. HIDDEN ASSUMPTIONS (not shown, I am assuming)
- lines is something you can loop over that gives strings, such as an open file or a list. If it is a file, each line ends with a newline; that does not break the match here.
- Every log line has a space after the status code. Your sample lines do.
5. GOTCHAS FOR ON-CALL
- A line that ends right after the status code (no size field and no trailing space) does not match and is skipped silently, so errors can go uncounted. Count skipped lines while testing.
- A status shown as "-" never matches, so those lines are skipped too.
- A double quote inside the request path can shift where .* lands; test with an odd URL from your real logs.
- Ties: when two IPs have the same count, their order follows the order Counter first saw them, not anything meaningful.
6. CONCEPTS
- Regex: a small pattern language for matching text.
- Capture group: the part in parentheses that you can pull out with group(1), group(2).
- Greedy .*: matches as much as possible, then backs off until the rest of the pattern fits.
- Counter.most_common(n): sorted top n counts.
7. CHECK YOURSELF
a) What does top_errors return if no line has a 5xx status?
b) Why is int() needed before status >= 500?
c) Would a line ending in 503 with no trailing space be counted?
Answers: a) an empty list []. b) group(2) is the string "502"; comparing to the number 500 needs a number. c) No; the pattern needs a space after the three digits, so the line is skipped.