Artificial intelligence can generate Python code from a description written in ordinary language. That does not mean the computer now understands your goal perfectly, nor does it mean the first program it produces will be correct. Effective AI-assisted programming is a process: describe the problem clearly, examine the proposed solution, run it, test it, and revise either the code or the prompt.
The prompt is the description you give the AI. A good programming prompt works much like a set of specifications given to a human programmer. It explains what the program should accomplish, what information it will receive, what result it should produce, and what limitations it must follow.
Begin with the goal, then add requirements
Start by stating the overall purpose of the program. Then make the requirements precise.
A vague prompt might say:
Write a Python temperature program.
This leaves many unanswered questions. Should the program convert Fahrenheit to Celsius, Celsius to Fahrenheit, or both? Should it ask the user for input? How should it display the answer?
A clearer prompt would be:
Write a Python program that asks the user for a temperature in Fahrenheit, converts it to Celsius, and displays the result. Use the formula
celsius = (fahrenheit - 32) * 5 / 9. Display the result rounded to one decimal place.
The second prompt identifies the language, the required input, the calculation, and the expected output. The AI has fewer decisions to guess about.
State the knowledge and techniques that may be used
There are often many ways to solve the same programming problem. In an introductory course, an AI may produce a solution using features you have not learned. You can prevent this by stating appropriate constraints.
For example:
Write a beginner-level Python program that asks for the user’s name and age, then prints how old the user will be next year. Use only
input(),int(), variables, arithmetic, andprint(). Do not define functions or import libraries.
Constraints are not merely restrictions. They help ensure that the proposed solution fits the assignment and that you can understand it.
Useful constraints might include:
- Use only concepts covered so far in class.
- Do not use loops, functions, lists, or libraries yet.
- Use meaningful variable names.
- Produce exactly three lines of output.
- Do not perform the calculation in advance; make Python calculate it.
- Explain the code after presenting it.
Provide examples of the desired behavior
Examples make a prompt much more precise. They show how inputs should be transformed into outputs and help expose details that might otherwise be ambiguous.
Consider this request:
Write a Python program that calculates the cost of movie tickets.
The AI cannot know the ticket price, the expected input, or the desired output format. Adding an example helps define the task:
Write a Python program that asks how many movie tickets the user wants. Each ticket costs $9.50. Calculate and display the total cost. For an input of
3, the program should displayTotal cost: $28.50. Use only variables,input(),int(), arithmetic,print(), and an f-string.
An example does not replace the requirements; it illustrates them. Good prompts often include both.
Break larger problems into smaller steps
AI systems are more likely to produce useful code when a task is focused. Instead of requesting an entire complicated program at once, divide it into manageable parts.
Suppose you eventually want a program that records several quiz scores and reports statistics. You might work through these smaller tasks:
- Ask the user for one quiz score and display it.
- Ask for three quiz scores and calculate their average.
- Determine the highest of the three scores.
- Display the average and highest score with clear labels.
- Test the program with several different sets of scores.
After each step, run the code and make sure you understand it. Building incrementally makes errors easier to find and helps you see how each new feature changes the program.
Avoid ambiguous words
Words such as it, this, appropriate, normal, large, or format nicely may have several possible meanings. Name the exact value, code, or behavior you mean.
Ambiguous:
Change it so the answer looks better.
Specific:
Change the final
print()statement so the average is displayed with exactly two digits after the decimal point and begins with the labelAverage:.
Ambiguous:
Fix the calculation.
Specific:
The program should calculate the area of a rectangle by multiplying
lengthbywidth, but it currently adds them. Correct that calculation without changing the input or output statements.
When asking about existing code, include the relevant code and describe what you expected to happen. If an error occurred, include the complete error message.
Treat generated code as a proposal
An AI response is not automatically a correct solution. It is a proposed solution that must be evaluated. The AI may misunderstand the prompt, use an inappropriate technique, omit a requirement, or produce code that works only for certain inputs.
Before accepting generated code, ask:
- Can I explain what every line does?
- Does it use only the techniques permitted by the assignment?
- Does it satisfy every stated requirement?
- Have I run it myself?
- Have I tested more than one input?
- What unusual or boundary inputs might reveal a problem?
Never submit or rely on code that you cannot explain. If part of a response is unfamiliar, ask for clarification:
Explain this program one statement at a time for a student who has just started learning Python. Do not introduce additional Python features.
You can also ask for a simpler solution:
Rewrite the program using only variables, arithmetic,
input(), andprint(). Do not use a function or a list.
Run and test the program
Reading code is not enough to establish that it works. Run it and compare its behavior with the requirements.
For a program that converts hours to minutes, test several cases:
- A typical value, such as
2 - A smaller value, such as
0.5, if decimals are allowed - Zero
- A value that might expose an incorrect calculation
Determine the expected result before running each test. If you do not know what the correct result should be, you cannot tell whether the generated program is correct.
If the program produces an error, give the AI useful evidence:
When I enter
2.5, the program reportsValueError: invalid literal for int() with base 10. I need the program to accept decimal numbers of hours. Explain the cause of the error and revise only the input conversion needed to fix it.
This is more useful than simply saying, “It doesn’t work.”
Revise the prompt and iterate
The first response is rarely the end of the process. Compare the code with the requirements, identify a specific deficiency, and request a focused revision.
For example:
The program calculates the correct total, but it displays too many decimal places. Revise the output so the monetary amount always has two digits after the decimal point. Keep the rest of the program unchanged.
Iteration does not mean repeatedly asking the same question and hoping for a better answer. It means using what you learned from the previous result to make the next instruction clearer.
Sometimes it is better to begin a new prompt. A long conversation can accumulate outdated assumptions and irrelevant code. When the task changes substantially, restate the current goal and requirements in a clean prompt.
A useful prompt structure
For many introductory Python problems, the following structure works well:
Goal: Describe what the program should accomplish.
Inputs: State what information the program receives.
Outputs: State exactly what it should display or return.
Requirements: List required calculations and behaviors.
Constraints: Identify the Python features that may or may not be used.
Examples: Provide at least one sample input and expected output.
Explanation: Ask for an explanation appropriate to your current experience.
Here is a complete example:
Write a beginner-level Python program that calculates the price of notebooks. Ask the user for the number of notebooks. Each notebook costs $2.75. Display the quantity and total price. If the user enters
4, the output should be4 notebooks cost $11.00. Use onlyinput(),int(), variables, multiplication, and an f-string. Do not use functions, loops, or imported libraries. After the code, explain each statement briefly.
The programmer remains responsible
AI can help propose code, explain unfamiliar syntax, suggest tests, and revise a solution. It cannot take responsibility for determining whether the result is correct, appropriate, or understood. That responsibility belongs to the programmer.
The essential AI-assisted programming cycle is:
- Specify the problem clearly.
- Generate a possible solution.
- Inspect the code and explain how it works.
- Run the program.
- Test it against expected results.
- Revise the prompt or code based on evidence.
The goal is not merely to obtain code that appears to work. The goal is to develop and verify a solution that you understand.
