Library › Coding AI › College students

Write a NumPy-style docstring that matches what the code does

Practice: Keeping the AI to your sources. About 5 minutes.

About this exercise

Your data-science club's library needs a docstring for moving_average before the release. Use NumPy style with exactly three sections in this order: Parameters, Returns, Raises (no Examples or Notes). It must describe what the code really does, not the old comment. Return only the docstring (no def line or body) in exactly 1 code block of at most 24 lines, with under 15 words outside it.

Common mistakes when you ask AI for this

  • Copying the stale comment. The comment still says the output is padded to the same length, and the AI repeats it. Try: Add: "Describe what the code does now, not the comment: the result has len(values) - window + 1 items."
  • Wrong style or extra sections. The AI often writes Google style or adds an Examples section. Try: Say: "NumPy style, exactly Parameters, Returns, Raises, in that order. No Examples."
  • Forgetting the error. The AI may skip Raises or get the condition wrong. Try: Add: "Raises ValueError if window < 1 or window > len(values)."
  • The whole function back. The AI pastes the full function with the docstring inside and a note. Try: Add: "Return only the docstring in one code block, no def line."

What a strong prompt includes

  • Names the style
  • Lists the sections
  • Says to trust the code
  • Rules out examples
  • Asks for only the docstring

Practice this now ↑

Related exercises