Library › Coding AI › Software developers

Fix a KeyError from a stack trace without touching anything else

Practice: Setting limits and rules. About 5 minutes.

About this exercise

The nightly report crashed with the stack trace below after the store switched to a new CSV export. Your lead decided two things: a missing or blank discount counts as 0, and rows with status 'refunded' must be skipped entirely. Team rule for hotfixes: every changed or added line ends with the comment # ORD-412. Return only the fixed summarize_orders function in exactly 1 code block of at most 16 lines, with the same name, argument and return keys, no new imports, and under 25 words of explanation.

Common mistakes when you ask AI for this

  • Getting the whole file back. Ask the AI to fix an error and it often reprints the entire script, so you have to diff it yourself to find what changed. Try: Say: "Return only the fixed summarize_orders function, nothing else."
  • Missing the blank-value case. The usual fix, .get("discount", 0), stops the KeyError but still crashes on the blank discount in row 1001. Try: Add: "Treat a missing or blank discount as 0, e.g. float(order.get('discount') or 0)."
  • Not passing on the team's decision. The AI can't know refunded rows should be skipped, so it keeps counting them. Try: Add: "Skip rows where status is 'refunded'."
  • Skipping the team's hotfix tag. The AI doesn't know your team tags hotfix lines, and it likes to rename things or add its own comments. Try: Add: "End each changed or added line with # ORD-412. Keep the name, argument and return keys."
  • Getting an essay. By default the AI explains the bug at length before and after the code. Try: Add: "No explanation, just the code block."

What a strong prompt includes

  • Asks for only the one function
  • Covers blank as well as missing
  • Says to skip refunded rows
  • Gives the hotfix tag
  • Limits the explanation

Practice this now ↑

Related exercises