🎓 A1Learner
🔍
0 of 50 lessons completed

Prompt Engineering: Ask for Code, Not Explanations

A code prompt asks the model to write code. The prompt must name the job, the input and the output.

A vague code prompt

The prompt asks for code but names no input and no output.

Write code for the shop orders.

A clear code prompt

The prompt names the language, the input and the output.

Write a Python function called order_status. It takes an order number and returns the order status as a string. The orders are 1042 shipped, 1043 processing and 1044 delivered.

A code prompt with the facts

The prompt gives the function the shop facts it needs.

Write a Python function called order_status. It takes an order number and returns the order status as a string. Use this small order table. Order 1042 is shipped. Order 1043 is processing. Order 1044 is delivered. Return unknown for any other order number.

Example

Write a Python function called order_status. It takes an order number and returns the order status as a string. Use this small order table. Order 1042 belongs to Maya and is shipped. Order 1043 belongs to Tom and is processing. Order 1044 belongs to Priya and is delivered. Return unknown for any other order number. Give only the code.

Key points

  • A code prompt asks for a working function, not an explanation of the job.
  • The prompt names the input the function takes and the output it returns.
  • The prompt names the language. A Python function is not a JavaScript function.
  • The prompt gives the small facts the function needs. The shop orders are the facts here.
  • A good code prompt says what the function must do and nothing more.

This example runs the code prompt against the local model and shows the function it wrote.

Note

The function the model writes is the shop's own code. It is not a library function.

Try it Yourself

Edit the code, then press Run.

Running is simulated · Rust Playground

Exercise

What must a code prompt name?