Basic Errors

Getting Started

Basic Errors

In this tutorial, we'll read common beginner errors without panic.

Every programmer gets errors. Errors are not a sign that you are bad at coding. They are messages from Python telling you what stopped the program.

At first, error messages can look scary. The trick is to read them slowly and look for the useful parts.

Syntax Errors

A syntax error means Python could not understand the code structure.

print("Hello"

This is missing a closing parenthesis. Python might show:

SyntaxError: '(' was never closed

The useful clue is was never closed.

Another common syntax error is forgetting a colon after if, for, or while:

if age >= 18
    print("Adult")

Correct version:

if age >= 18:
    print("Adult")

Name Errors

A name error usually means you used a variable before creating it, or you misspelled it.

name = "Lina"
print(nmae)

Python sees nmae, but only name exists.

Correct version:

name = "Lina"
print(name)

Type Errors

A type error means you tried to use a value in a way that does not make sense for its type.

age = 10
print("Age: " + age)

Python cannot join a string and an integer with +. One fix is to use commas in print():

age = 10
print("Age:", age)

Reading a Traceback

A traceback often includes:

  • The file name
  • The line number
  • The kind of error
  • A short explanation

Start with the line number. Look at that line first, then look at the line just above it.

Practice

Copy each broken example from this lesson into a file, run it, read the error, and then fix it.