Writing a prompt for code generation

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Writing a prompt for code generation

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which prompt is better for an AI code generator?
A Write a Python function that takes a list of marks and returns the average, rounded to 1 decimal place.
B Make me something to do with marks.
2. A student wants JavaScript code. What should the prompt include?
A The language name, such as 'Write a JavaScript function...'
B Nothing about the language, because the AI will always guess correctly.
3. Which part of a prompt describes the expected output?
A Use a function that takes a price.
B Return the total price including 10% GST as a number.
4. Which is an example input and output you could add to a prompt?
A Example: the code should be good and work well.
B Example: input 5 returns 120 (the factorial of 5).
5. Which is a constraint you could include in a prompt?
A Do not use any external libraries and keep it under 20 lines.
B Write the code however you like.

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1Write a Python function that takes a list of marks AToo vague: it does not state the language, the inputs, the expected output or any constraints.
2Do not use any external libraries. BUpdate: also return 0 if the list is empty.
3Give an example input and output so the AI can see exactly what is expected CExample: input [70, 80, 90] should return 80.0
4Refine the prompt when the first result is not right by adding the missing requirement DIt leaves out the input and an example, so the AI does not know what data is given or what the result should look like.
5Make me a program for my marks. EA constraint that limits how the generator may write the code
6Write a Python function to find the average. Return it. FStates the language (Python) and the input (a list of marks) so the generator knows what to produce

Definitions

prompt

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
Language: Python _____: a list of student marks Output: the average mark
2.
Language: Python Input: a list of student marks ______: the average mark
3.
Language: Python Task: square a number _______: input 3 gives output 9
4.
Language: Python Task: find the highest mark in a list __________: do not use the max() function
5.
Code missed my rules, so I will ______ the prompt and add an example.

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A Language: Python
B Input: a list of marks
C Output: the average mark
D Example: [80, 90] -> 95.0
E Constraint: use no modules
Question 2
A Language: JavaScript
B Input: whatever is needed
C Output: a greeting string
D Example: 'Zoe' -> 'Hi Zoe'
Question 3
A Write a function
B Input: two numbers
C Output: the larger number
Question 4
A Language: Python
B Input: a temperature in C
C Output: temperature in F
D Constraint: any rounding is fine
Question 5
A Language: Python
B Input: a list of words
C Output: True if n is even
D Example: 4 -> True, 7 -> False

Answer Key

Writing a prompt for code generation
Section A: Quick Questions
Q1
A — Write a Python function that takes a list of marks and returns the average, rounded to 1 decimal place.
Q2
A — The language name, such as 'Write a JavaScript function...'
Q3
B — Return the total price including 10% GST as a number.
Q4
B — Example: input 5 returns 120 (the factorial of 5).
Q5
A — Do not use any external libraries and keep it under 20 lines.
Section B: Match the Pairs
Q1
Write a Python function that takes a list of marks → States the language (Python) and the input (a list of marks) so the generator knows what to produce
Q2
Do not use any external libraries. → A constraint that limits how the generator may write the code
Q3
Give an example input and output so the AI can see exactly what is expected → Example: input [70, 80, 90] should return 80.0
Q4
Refine the prompt when the first result is not right by adding the missing requirement → Update: also return 0 if the list is empty.
Q5
Make me a program for my marks. → Too vague: it does not state the language, the inputs, the expected output or any constraints.
Q6
Write a Python function to find the average. Return it. → It leaves out the input and an example, so the AI does not know what data is given or what the result should look like.
Section C: Definitions
Q1
prompt — Instructions given to an AI code generator. A good one states the language, the inputs, the expected output and any constraints so the code matches the requirements.
Section D: Fill in the Blanks
Q1
Language: Python Input: a list of student marks Output: the average mark
Q2
Language: Python Input: a list of student marks Output: the average mark
Q3
Language: Python Task: square a number Example: input 3 gives output 9
Q4
Language: Python Task: find the highest mark in a list Constraint: do not use the max() function
Q5
Code missed my rules, so I will refine the prompt and add an example.
Section E: Spot the Error
Q1
D — The example output must match the requirement. The average of 80 and 90 is 85.0, not 95.0. A wrong example makes the generator produce wrong code.
Q2
B — The input is vague. A good prompt states exactly what the input is, such as 'a string, the name', so the generator knows what the function receives.
Q3
A — The prompt leaves out the programming language. It should say 'Write a Python function' so the generator uses the right language.
Q4
D — A constraint should be specific. 'Any rounding is fine' gives the generator no rule to follow. It should say 'Constraint: round to 1 decimal'.
Q5
B — The input description does not match the output and example, which use whole numbers like 4 and 7. It should be 'Input: a whole number n'.