Python for AIPython for AI
Python Basics

Python errors

Understanding when code doesn't run

Even the best programmers encounter errors. Understanding errors is a core part of learning to code. Let's make friends with them instead of fearing them.

What happens when code breaks?

When your Python code can't run, it stops. Instead of continuing, it tells you what went wrong. This message is called an "error" or "exception".

Think of it like following a recipe. If the recipe tells you to add "flubber" and you don't know what that is, you can't continue. The recipe has a problem.

Your first error

Let's try to make a simple error on purpose. Open your Python interpreter or a new Python file and type this in, then run it:

python
print("Hello world!)

Oops! Python points out that something is wrong. You probably got a SyntaxError, which means you broke a rule for how Python code should be written. In this case, we forgot the closing double quote.

Code challenge
Not started
Write code that prints exactly: Hello world!

What's next?

Nice work! Once you're comfortable spotting syntax errors, you're ready to store and work with data. The next lessons cover variables, operators, and strings — the building blocks of real programs.

How to read errors

Python error messages might look scary at first, but they have a lot of useful information. Let's break down a common one:

text
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'my_variable' is not defined
  • Traceback: This shows the sequence of function calls that led to the error. For simple scripts, it's short, but it can be long for complex programs.
  • File "<stdin>", line 1: This tells you where the error happened. <stdin> means it was typed directly into the interpreter, and line 1 is the exact line.
  • NameError: This is the *type* of error. Python has many error types. This one means you tried to use a variable name (my_variable) that hasn't been created yet.
  • name 'my_variable' is not defined: This is the specific error message. It explains *why* the NameError occurred.
The last line of the traceback, containing the error type and message, is usually the most helpful for understanding what went wrong.

Errors are normal

Seriously, every coder makes mistakes. Errors are not a sign of failure; they are signs that you are trying things out and learning. They are part of the coding process.

Think of errors as friendly guides. They don't scold you; they just point you in the right direction to fix your code.

AI helps with errors

Tools like ChatGPT or other AI assistants can be incredibly helpful when you encounter an error you don't understand. Just copy and paste the entire error message into the AI.

The AI can often explain what the error means in simpler terms, suggest common causes, and even provide example fixes. Remember to still try to understand the explanation yourself, even with AI's help.

Practice

Try these exercises to lock in what you just learned.

Quiz
Not started
Using a variable that hasn't been defined raises which error?
Fill in the blanks
Not started
Fix the missing piece so this is valid Python.
1
2 print("ok")
Code challenge
Not started
Catch the ZeroDivisionError and print exactly: oops

What's next?

Now that you're comfortable with errors, you can move on to writing more Python code. Don't be afraid to experiment, and if something breaks, you know how to start looking for the solution.