Exercise 8: Load Records, Skipping and Reporting the Bad Ones — Possible Solution ==================================================================== rows = [ "Ann,34,Leeds", "Bo,twenty,York", "Cyd,41", "Dee,29,Bath,UK", "Eli,52,Hull", ] good = [] errors = [] for number, row in enumerate(rows, start=1): fields = row.split(",") if len(fields) != 3: errors.append(f"row {number}: expected 3 fields, found {len(fields)}") continue name, age, city = fields if not age.isdigit(): errors.append(f"row {number}: age {age!r} is not a whole number") continue good.append((name, int(age), city)) print("good:") for record in good: print(" ", record) print("errors:") for message in errors: print(" ", message) Example run: good: ('Ann', 34, 'Leeds') ('Eli', 52, 'Hull') errors: row 2: age 'twenty' is not a whole number row 3: expected 3 fields, found 2 row 4: expected 3 fields, found 4 WHY THIS WORKS AS AN ANSWER ------------------------------ Rows are checked in an order that makes the next check safe: the field count is tested before the row is unpacked into name, age and city (unpacking the wrong number of fields would raise ValueError), and the age is tested before int() is called. Each failed check records a message and continues. enumerate(rows, start=1) provides the row numbers, and {age!r} shows the bad value with quotes so an empty or odd value is visible. Collecting the errors, and not printing them in the loop, lets the report come out in a tidy block after the good records.