Writing a prompt to generate code

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

Writing a prompt to generate code

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which prompt is better for getting usable code from an AI tool?
A Write a Python 3 function that returns the average of a list of numbers.
B Make me something that works with numbers.
2. Which part of a prompt names the language and version?
A Write a function that is really good...
B Using Python 3.12, write a function...
3. Which prompt gives example inputs and outputs?
A Write a Python function is_even(n). For example, is_even(4) returns True and is_even(7) returns False.
B Write a Python function is_even(n) that does the right thing.
4. Which prompt follows the 'one task per prompt' guideline?
A Write a Python function that validates an age is between 0 and 120.
B Write a Python program with a login screen, a database, a validation function, a report and a file saver.
5. Which prompt states a constraint?
A Write a Python function to sort a list.
B Write a Python function to sort a list without using the built-in sorted() or .sort().

Match the Pairs

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

Left columnRight column
1Write a Python 3.12 function that returns the average of a list of numbers. AThe prompt is vague: it has no language, no clear task, no inputs or outputs, and no constraints.
2Example: average([2, 4, 6]) should return 4.0 BWrite a Python function called get_total that adds up a list of prices.
3A prompt should state constraints, such as which features to use or avoid and how to handle bad input. CAn example input and expected output added to a prompt so the AI knows exactly what the code must do.
4A prompt should name the language so the AI generates code in the right language. DDo not use any libraries. If the list is empty, return 0.
5Write some code that does stuff with numbers. EA prompt that names the language and version, states one task, and describes the input (a list) and the output (an average).
6Write a Python program that reads a file, sorts the lines, validates the data, and draws a graph. FIt asks for several tasks in one prompt instead of one task per prompt.

Definitions

code prompt

Fill in the Blanks

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

1.
Write a ______ function that returns the average of a list.
2.
Write a Python function that returns the average of a ____ of numbers.
3.
Example input: [2, 4, 6] Example ______: 4.0
4.
Write a Python function to average a list. Constraint: return 0 if the list is _____.
5.
Python 3: write ___ function that finds the largest number in a list.

Spot the Error

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

Question 1
A Write a Python 3 function to
B find the average of a list.
C Input: a list of numbers.
D Output: something useful.
Question 2
A Write a function that
B returns the largest number.
C Input: [3, 9, 2]
D Output: 9
Question 3
A Write a Python 3 function to
B counts vowels and saves a CSV
C Input: 'banana'
D Output: 3
Question 4
A Write a Python function that
B returns the average of nums.
C Example: [2, 4, 6] gives 9.0
D Do not use any libraries.
Question 5
A Write a Python 3 function to
B check if a password is valid.
C Input: string; output: bool
D Make the rules strong enough.

Answer Key

Writing a prompt to generate code
Section A: Quick Questions
Q1
A — Write a Python 3 function that returns the average of a list of numbers.
Q2
B — Using Python 3.12, write a function...
Q3
A — Write a Python function is_even(n). For example, is_even(4) returns True and is_even(7) returns False.
Q4
A — Write a Python function that validates an age is between 0 and 120.
Q5
B — Write a Python function to sort a list without using the built-in sorted() or .sort().
Section B: Match the Pairs
Q1
Write a Python 3.12 function that returns the average of a list of numbers. → A prompt that names the language and version, states one task, and describes the input (a list) and the output (an average).
Q2
Example: average([2, 4, 6]) should return 4.0 → An example input and expected output added to a prompt so the AI knows exactly what the code must do.
Q3
A prompt should state constraints, such as which features to use or avoid and how to handle bad input. → Do not use any libraries. If the list is empty, return 0.
Q4
A prompt should name the language so the AI generates code in the right language. → Write a Python function called get_total that adds up a list of prices.
Q5
Write some code that does stuff with numbers. → The prompt is vague: it has no language, no clear task, no inputs or outputs, and no constraints.
Q6
Write a Python program that reads a file, sorts the lines, validates the data, and draws a graph. → It asks for several tasks in one prompt instead of one task per prompt.
Section C: Definitions
Q1
code prompt — A clear AI request stating the language, the task, the inputs and outputs, and the constraints. Keep to one task per prompt, e.g. Write a Python function that returns an average.
Section D: Fill in the Blanks
Q1
Write a Python function that returns the average of a list.
Q2
Write a Python function that returns the average of a list of numbers.
Q3
Example input: [2, 4, 6] Example output: 4.0
Q4
Write a Python function to average a list. Constraint: return 0 if the list is empty.
Q5
Python 3: write one function that finds the largest number in a list.
Section E: Spot the Error
Q1
D — The output is vague. A good code prompt says exactly what comes out, for example 'Output: average as a float.'
Q2
A — The prompt does not name the programming language. It should say 'Write a Python function that' so the AI does not guess.
Q3
B — This line asks for two tasks (count vowels and save a CSV). A good prompt has one task, so it should be 'counts vowels in a string.'
Q4
C — The example output is wrong. The average of 2, 4 and 6 is 4.0, so the example would mislead the AI.
Q5
D — The constraint is vague and cannot be measured. It should state a specific requirement, such as 'Must be 8+ characters long.'