Library › Coding AI › Product managers

Compute NPS from a survey export with plain Python

Practice: Setting limits and rules. About 5 minutes.

About this exercise

Write a Python script using only the standard library that reads survey.csv and prints one line exactly like NPS: 32 (n=118). Promoters score 9-10 and detractors 0-6. Rows with a blank score are skipped and don't count toward n. NPS is percent promoters minus percent detractors, rounded to a whole number. One code block, at most 20 lines, then under 25 words outside the code.

Common mistakes when you ask AI for this

  • Not saying what to do with blanks. The AI counts blank scores as zeros or as responses, which drags NPS down. Try: Add: "Skip blank scores; they don't count in n."
  • Trusting the AI's definition. The AI may count 7 or 8 somewhere or forget to round. Try: Add: "Promoters 9-10, detractors 0-6, round to a whole number."
  • Not showing the output. The AI prints a report with percentages and labels you don't need. Try: Add: "Print one line exactly like NPS: 32 (n=118)."
  • Getting a menu of options. The AI likes to offer two or three versions of the script, so you're left to pick and test them yourself. Try: Add: "Give one script only, in one code block, no alternatives."
  • Getting a tutorial. By default the AI explains every part of the code at length, which you then have to scroll past. Try: Add: "After the code, one line under 12 words. Nothing else."

What a strong prompt includes

  • Handles blank scores
  • Defines the groups
  • Shows the output line
  • Rules out pandas
  • Caps the code length

Practice this now ↑

Related exercises