Appendix 3: Using AI in Coding#
Danger
Never run code or programs suggested by AI models without carefully reviewing them first. Failure to do so may lead to breakage of your system, data loss, or security vulnerabilities.
You have been warned.
Introduction#
In the last decade, and particularly in the last 2 years, artificial intelligence (AI) has had a large impact on many areas. Coding has been particularly affected, as the latest AI models are capable of generating increasingly complex code fragments from simple language requests. These tools can be particularly useful for beginner programmers, who in most cases would be looking to write or modify relatively simple code. Therefore, this appendix is here to help you to explore these AI models, hopefully enabling you to tackle larger and more complicated projects than you otherwise might initially dare to!
There are several things to keep in mind related to the use of these models:
They perform best when the request is explained as clearly as possibly, with as much context and specifics as possible.
For now, the AI models perform best in writing relatively small sections of code - up to around 200 lines of code. Therefore, when tackling something even larger than this, it is worthwhile to split the program in smaller chunks and request AI to help write these one after another.
While it is becoming less of an obvious problem, the AI models can still “hallucinate”, meaning they might confidently reference packages that don’t exist (or might now be malicious) or functions that aren’t available in the libraries they mention.
Human programmer oversight and understanding of the coding project is still an integral component in this process, and therefore you still need to think about what the code generated will actually do.
These models are still evolving rapidly, and therefore the capabilities described here may change quickly over time. Always think critically about how to best use these tools.
Available AI Tools for Coding#
There are many AI providers you might consider. As of early 2026, some commonly used ones include:
You will also find AI coding assistants integrated into code editors, such as GitHub Copilot for VS Code, which is the recommended code editor and AI interface for this course. There are some instructions in Appendix 3b on how to get started with GitHub Copilot specifically.
Getting Started with AI for Coding#
Below are some step-by-step instructions to help you learn how to use AI effectively for coding:
Step 1: Try a Simple Project First#
Try asking the AI to write code for a molecular weight calculator. Be specific in your request, for example:
“Write a Python program that calculates the molecular weight of chemical compounds. The user should be able to input a chemical formula like H2O or C6H12O6, and the program should output the molecular weight.”
Copy and paste the code into VS Code and try running it. Does it work? You may need to install any packages that the code requires using pip, as we have seen already.
Step 2: Refine Your Request#
These tools are very interactive - you can always request certain changes to the code:
Ask AI to modify the code to avoid using external packages: “Can you rewrite this without using any external packages?”
Request specific programming constructs: “Can you modify this to use only ‘for’ loops instead of ‘while’ loops?”
Ask for additional features: “Could you add a graphical user interface to make data input easier?” (This will likely require installing additional packages like PyQt or Tkinter)
Step 3: Troubleshooting with AI#
If you encounter errors when running the code:
Copy the exact error message
Paste it back into your conversation with the AI
Ask for help understanding and fixing it: “I got this error when running the code. What does it mean and how can I fix it?”
The AI will often be able to explain what went wrong and provide corrected code.
Step 4: Deepen Your Understanding#
Don’t just copy and use the code - ask the AI to explain it:
“Can you explain how the molecular weight calculation works in this code?”
“What does this specific line of code do?”
“How does this function handle the parsing of chemical formulas?”
Understanding the code you’re using is essential, especially since code understanding will be a major component of your final project assessment.
Step 5: Try Something More Complex#
Once you’re comfortable with the process:
Start a new conversation with the AI
Ask it to write code for a more complex project, perhaps one of the project ideas that were offered to you
Be detailed in your request, specifying what functionality you need and any specific requirements
Step 6: Test and Iterate#
Test the code to see if it runs correctly
Install any additional packages needed
Debug any issues by conversing with the AI
Ask for improvements or additional features as needed
Tips for Effective AI Coding Assistance#
Be specific: The more details you provide about what you want the code to do, the better the results will be
Start simple, then expand: Begin with a basic version of your program and gradually add features
Ask for explanations: Always make sure you understand how the code works
Verify the output: Don’t assume the code is correct - test it with various inputs
Learn from the code: Use the AI-generated code as a learning tool, not just a solution