Part 4 - Getting It Right
17. Prompting for Code and Technical Help
Goal: Get working code and technical explanations by prompting precisely.
AI is great for coding help, but only if you state the language, what the code should do, the inputs and outputs, and any constraints. Vague coding prompts produce code that runs but does the wrong thing.
Always ask the AI to explain the code and how to test it. You should understand what you paste into your project, and you must run it to confirm it actually works.
The Script (Template)
In [language], write a function that [does X] given [inputs] and returns [outputs]. [Constraints]. Explain how it works and how to test it.
Worked Example
You write
In Python, write a function that takes a list of numbers and returns the average. Handle an empty list by returning 0. Explain it briefly.
AI replies (sample)
def average(numbers):
if not numbers: # empty list -> avoid divide by zero
return 0
return sum(numbers) / len(numbers)
It adds all the numbers with sum() and divides by how many there are with len(). If the list is empty it returns 0 instead of crashing. Test: average([2, 4, 6]) returns 4.0; average([]) returns 0.Why it works
Naming the language, the input, the output and the tricky edge case (empty list) got correct, safe code plus a test you can run to confirm it.
Do & Avoid
Tips
- State the language, inputs, outputs and edge cases explicitly.
- Always run and test AI code; ask for test examples in the prompt.
Common Mistakes
- Asking for code without saying what it should do with unusual inputs.
- Pasting code you have not run or do not understand.
Practice
- Ask for a small function with a stated edge case, then run it.
- Paste an error message and ask the AI to explain and fix it.
My Practice — capture your own script
Write your own version of this lesson's script, run it in your AI chat, then paste the reply. Your work is saved on this device; use Download to keep a copy.