L0-008 Dict vs list for a parsed line
Rule: a dict looks a field up by name (row["severity"]), a list by position (row[2]). A dict does not protect by itself: the positions live in one place, inside parse_line, so fixing that one line fixes every caller that uses names.
Predict, then run: parse_line(...)[2] on the dict gives KeyError: 2 (keys are names). On "7,23,25" the position-based parse_line silently gives 23 for result["Thermostat_stop"] (wrong value, no error); a list version's result[1] gives 23 too.
Common mistake / drill: adding a field at the front of the line shifts positions and breaks list indexes.
Self-test: in my own words, why is a dict more specific than a list?
Confirmed 2026-11-05 · L0 W1
L0-009 int() changes only the value it wraps
Rule: wrapping one value in int() converts only that value.
My working code: {"Start_thermostat": int(my_list[0]), "Stop thermostat": my_list[1]} on "23,25" printed {'Start_thermostat': 23, 'Stop thermostat': '25'}.
Common mistake / drill: the second value stays a string until it also gets int().
Self-test: which value lost its quotes, and why?
Confirmed 2026-11-04 · L0 W1
L0-010 Mixed types crash arithmetic
Rule: an int and a string cannot be subtracted.
Predict, then run: result["Stop thermostat"] - result["Start_thermostat"] with a string and an int gives TypeError: unsupported operand type(s) for -: 'str' and 'int'. With int(my_list[1]) added the subtraction printed 2.
Self-test: how do you read that TypeError?
Confirmed 2026-11-04 · L0 W1
L0-011 Convert at the point of entry, fail early
Rule: convert the type the first time the value is defined.
Predict, then run: with int() on both values inside parse_temperature, "abc,25" gives ValueError: invalid literal for int() with base 10: 'abc' at the int(my_list[0]) line, before any subtraction.
Common mistake / drill: I predicted the crash would come at the subtraction (wrong). Listed in Not yet solid.
Self-test: where does the crash appear, and why there?
Confirmed 2026-11-04 · L0 W1
L0-012 Quiet skip vs loud crash
Rule: a parser that silently skips or replaces bad lines hides data loss. My wording: a quiet skip is more dangerous than knowing there is an issue, because the final result contains missing data.
Self-test: why is a loud crash safer here?
Confirmed 2026-11-04 · L0 W1
L0-013 Dict keys must match exactly
Rule: a key must be called the same way it was written; the display spacing belongs in the print, not in the key.
My working code:
def parse_line(line):
my_line = line.split(",")
my_dic = {"Thermostat_start": int(my_line[0]), "Thermostat_stop": int(my_line[1])}
return my_dic
print("Thermostat is starting: ", parse_line("23,25")["Thermostat_start"]) # Thermostat is starting: 23
Common mistake / drill: ",".split(line) (receiver and argument swapped) gave ValueError: invalid literal for int() with base 10: ','. Keys written as "Thermostat_start: " printed as {'Thermostat_start: ': 23, ...}. Drill: timed rewrite from memory.
Self-test: why does a lookup fail when a key has a colon inside the quotes?
Confirmed 2026-11-05 · L0 W1
L0-014 A line without a comma gives a one-item list
Rule: "23".split(",") gives ['23'] with no error when the separator is not found.
Predict, then run: parse_line("23"): int(my_line[0]) works, int(my_line[1]) raises IndexError: list index out of range at that line.
Rule of thought: the failure is a missing position, not a bad value, so it is not a ValueError.
Self-test: why IndexError and not ValueError?
Confirmed 2026-11-05 · L0 W1
L0-015 Three different errors for bad input
Rule: int() accepts a string only if it holds digits, optionally with a sign in front like -5. Verify exact rules in the current Python docs.
Predict, then run: "23" gives IndexError (position missing). "23," gives ['23', ''], then ValueError: invalid literal for int() with base 10: ''. len('') is 0 (an empty string is a real string, not None). int(None) gives TypeError. int('2 3') and int('2.5') give ValueError; int('-5') gives -5; int(2.5) (a float) gives 2.
Common mistake / drill: I predicted 0 for "23," (wrong). Run the checks before guessing.
Self-test: which error for a missing position, which for a non-number?
Confirmed 2026-11-05 · L0 W1
L0-016 Extra fields at the front: no error, wrong values
Rule: a function expecting fewer positions than the input has still runs; one expecting more fails. The hard-coded positions cause the wrong pairing, not the key names.
Predict, then run: "E101,Pump overheating,high" gives {'Code': 'E101', 'Description': 'Pump overheating', 'Priority': 'high'}. "7,E101,Pump overheating,high" gives {'Code': '7', 'Description': 'E101', 'Priority': 'Pump overheating'}: no error, wrong values, 'high' dropped.
Common mistake / drill: I predicted a failure (wrong, it ran). Quiet wrong output is more dangerous than a traceback.
Self-test: why does this run without error?
Confirmed 2026-11-06 · L0 W1
L0-017 A returned error message does not stop the program
Rule: returning an error text from the else branch hides the problem; the crash comes later, far from the cause.
Predict, then run: with if len(my_line) == 3: (exact count) building the dict and else: returning a text, collect_status["Priority"] raised TypeError: string indices must be integers, not 'str' at the print line, with no mention of the bad input. A loop over three lines ran to the end and put the message string between two dicts in the result list.
Self-test: why does the error show up at the print and not in parse_line?
Confirmed 2026-11-06 · L0 W1
L0-018 raise stops the program at the cause; break needs a loop
Rule: raise stops at the cause and the traceback points inside the function. break only works inside a for or while loop.
My working code (from the tutorial "Raising Exceptions"): raise IndexError("The indexes of the list must be 2") in the else branch.
Predict, then run: "20,25,26" gives a traceback with File ".../app.py", line 6, in parse_line and IndexError: The indexes of the list must be 2.
Common mistake / drill: break in the else gave SyntaxError: 'break' outside loop.
Self-test: why does the traceback now point at line 6 and not at a later print?
Confirmed 2026-11-08 · L0 W1
L0-019 A function can fail loudly for two reasons
Rule: wrong part count raises my own error; non-number text raises Python's ValueError from int().
Predict, then run: without int(), parse_line("abc,25") ran and kept 'abc' as a string. With int() inside the dict plus my raise in the else, the same call stops with ValueError: invalid literal for int() with base 10: 'abc' at line 4.
Common mistake / drill: a NameError from a typo (my_linne), fixed by reading the traceback.
Self-test: which line raises which error?
Confirmed 2026-11-08 · L0 W1
L0-020 Type vs value: ValueError is "right type, bad value"
**Rule (from the Python docs for ValueError):** right type but an inappropriate value. My wording: int('abc') fails because the string is fine but the value is not.
My working code:
raise ValueError(f"You have provided {len(my_line)}, the number of strings should be 2!")
Predict, then run: on "20,25,26" the output ends ValueError: You have provided 3, the number of strings should be 2! at line 7, one plain sentence. Separate arguments (raise ValueError("a", len(my_line), "b")) display as a tuple with parentheses and quotes. print(type("20,25,26")) gives <class 'str'>; .split(",") gives <class 'list'>. my_line[2] on a 3-item list gives 26; my_line[3] raises IndexError.
Common mistake / drill: type('20','25','26') gives TypeError (three arguments); use one string with one pair of quotes. The same message for "20,25,26" and "23" could not tell the inputs apart, which is why I added the count.
Self-test: why ValueError for 'abc' but IndexError for a missing position?
Confirmed 2026-11-08 · L0 W1