Prompt Engineering Tutorial menu

Prompts for Coding

AI coding assistants are most useful when you give them the language, the goal, the code and the exact error.

The Coding Prompt Checklist

IncludeWhy
Language and versionPython 3, JavaScript, PHP... the code differs
What the code should doThe goal, with an example input and output
Your existing codeSo the AI fits its answer to it
The full error messageThe exact text, not "it doesn't work"
Limits"No extra libraries", "beginner-friendly", "keep my variable names"

Writing New Code

Example

text
Write a Python function called grade(marks) that returns
"A" for 80 and above, "B" for 65-79, "C" for 50-64 and "F" below 50.
Add 3 example calls with their expected results. No libraries.

A good reply might look like this, and you should run it yourself:

Example

python
def grade(marks):
    if marks >= 80:
        return "A"
    elif marks >= 65:
        return "B"
    elif marks >= 50:
        return "C"
    return "F"

print(grade(85), grade(70), grade(42))
Output
A B F

Fixing an Error

Paste the code and the full error:

Example

text
My Python code crashes. Explain the cause in simple words
and show the fixed line only.

Code:
price = input("Price: ")
total = price * 2 + 50

Error:
TypeError: can only concatenate str (not "int") to str

The AI should explain that input() returns text, so it must be converted with int() or float():

Example

python
price = int("120")   # in the real program: int(input("Price: "))
total = price * 2 + 50
print(total)
Output
290

Explaining Code

Example

text
Explain this code line by line for a beginner.
Then tell me one way it could break.

for i in range(1, 6):
    print(i * "*")

Reviewing and Improving

Example

text
Act as a senior Python developer. Review my function for bugs,
unclear names and missing edge cases. Give a numbered list of
issues, most important first. Do not rewrite it yet.

Then follow up: "Now fix issues 1 and 2 only."

Asking for Tests

Example

text
Write 5 test cases for my grade() function, including the edge
values 49, 50, 79 and 80. Show each input and expected output.

Edge values are where most bugs hide.

Safe Habits with AI Code

  • Run and test everything. AI code can look right and still be wrong.
  • Understand it before you submit or deploy it. You are responsible for it.
  • Never paste secrets: remove API keys, passwords and database URLs before sharing code.
  • Check packages: AI sometimes suggests libraries or functions that do not exist. Look them up.
  • Ask for small pieces: one function at a time is easier to check than a whole app.

Coding assistants inside editors (like VS Code) can read your project files. Give them clear instructions, review every change before accepting it, and keep your code in Git so you can undo mistakes.

Exercise

Exercise

Your code shows an error. What should your prompt include?

Learn Prompt Engineering with a teacher

Join the AI & Prompt Engineering course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp