Skip to content

Lesson 12 ยท Files, Data and Code

Coding With ChatGPT: Explain, Write and Debug

Use ChatGPT to explain, write, test and debug code with clear context, small steps and your own tests, and learn when to use Codex for repository work.

ChatGPT can explain code, suggest fixes, write small programs and help you learn a language. It can also produce code that looks right and fails, uses functions that do not exist or has security holes. Treat generated code like code from a stranger: read it, test it and understand it before you use it.

If you are new to programming, the Python tutorial gives you basics that make this lesson more useful.

Give the facts a developer needs

A good coding request includes:

  • The language and version.
  • What the code should do, with an example input and output.
  • The exact error message, copied in full.
  • The relevant code, not your whole project.
  • Limits, such as "standard library only".
TEXT
Language: Python 3.
Goal: read a list of prices as text and return their total.
Problem: crashes on "4,50" with ValueError: could not convert
string to float: '4,50'
Question: explain the cause first, then suggest a fix. Standard
library only. Tell me what inputs the fix still does not handle.

The last sentence asks about the limits of the fix. That often reveals edge cases.

Ask for explanations

For learning, ask for a line-by-line explanation, or ask ChatGPT to quiz you:

TEXT
Explain this function to a beginner. Then give me two inputs and ask
me to predict the output before you reveal it.

Predicting the output builds real skill.

Test before you trust

Tests turn "looks right" into evidence. Suppose a reply suggested the function below (an illustrative suggestion written for this lesson). Before using it, you write tests, including awkward ones.

Python Test a suggested function yourself

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The first three pass. The fourth fails, because the replacement turns "1,234.50" into text that is not a number. A comma means different things in different countries, so the right fix depends on your data. Paste the failing test back into the chat:

TEXT
This input fails: "1,234.50". My data uses a comma for thousands.
Please fix it, and keep the other cases passing.

Then run all cases again. Notice that your new rule changes what "4,50" should mean, so you must decide.

Debug in small steps

When something breaks:

  1. Reproduce the problem with the smallest example.
  2. Share the error and what you expected.
  3. Ask for likely causes, ranked, and how to test each.
  4. Change one thing at a time and run it again.

If suggestions go in circles, start a new chat with the current code and a short list of what you already tried.

Watch for common problems

Risk What to do
Invented functions or options Check the official documentation
Outdated APIs State the version, then verify
Unsafe code, such as SQL built from user text Ask for safe patterns and learn why
Destructive commands Do not run what you do not understand
Unknown packages Confirm the package exists and is trustworthy before installing

Never paste secrets into a chat. Replace keys and passwords with placeholders. Do not share proprietary code unless your organization allows it. For database code, ask for parameterized queries, which keep user input separate from the SQL command.

Running code inside ChatGPT

For some tasks ChatGPT can run Python in its analysis environment, which helps verify a calculation or test a function. It has no external network access, and its setup is not the same as your computer. Passing there does not prove the code works in your project.

When you need Codex

OpenAI also offers Codex, a separate experience for software development: writing and debugging code, running tests, reviewing changes and working with repositories. Chat suits questions and small snippets. Codex suits tasks that touch many files. Whichever you use, review every change before you merge or deploy it.

Learn, do not only copy

If you are studying, try the task yourself first, then ask for a review:

TEXT
Here is my solution. Do not rewrite it. Point out the two most
important improvements and ask me to make them.

A safe workflow for a small change

Work in this order. First, describe the current behavior and the desired behavior. Second, ask for a plan in plain words and read it. Third, ask for the smallest change that fulfills the plan. Fourth, add or update a test that fails before the change and passes after it. Fifth, run your whole test set and review the difference line by line. Commit only what you understand.

This order keeps each step reversible. If the plan is wrong, you lose a minute, not an afternoon.

Questions to ask about suggested code

TEXT
What inputs would break this?
What does it assume about the data?
Which parts depend on a specific library version?
What could go wrong if two users run it at the same time?

Treat the answers as a list of things to test, not as proof that nothing else can fail. For security-sensitive code such as login, payments or file uploads, ask an experienced reviewer, and rely on well-tested libraries instead of new custom code.

When the answer does not work

Report exactly what happened: the command you ran, the full output and what you expected instead. "It does not work" gives nothing to reason about. If an error mentions a function or setting that you cannot find in the official documentation, say so, and ask for a solution that uses only documented features of your version. Keep a copy of the last working code, so you can always return to it. Version control such as Git makes this easy and shows exactly what each suggestion changed.

Write down what finally fixed the problem and why. That note helps you the next time a similar error appears, and it checks that you understood the solution.

Common mistake: pasting a command into a terminal because it came from ChatGPT. Understand what it changes first, especially anything that deletes files or touches a database.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Which detail helps most when debugging?

    The exact error and a minimal case point to the cause. Never share passwords.
  2. 2. Generated code passes your first example. What next?

    One passing case is weak evidence.
  3. 3. A reply suggests installing a package you never heard of. What should you do?

    Suggested package names can be wrong or unsafe.

Summary

  • Give language, version, goal, errors and constraints.
  • Test generated code with ordinary and edge cases.
  • Check functions and packages against official sources.
  • Protect secrets and proprietary code.
  • Use Codex for larger repository tasks and review every change.

Next: Searching the Web With ChatGPT

Your progress is saved in this browser. No account needed.