loading...

Charming the Python: Error Types

vickilanger profile image Vicki (she/her) Updated on ・3 min read

If coding tutorials with math examples are the bane of your existence, keep reading. This series uses relatable examples like dogs and cats.


Error Types

Fixing errors is called debugging. Here are some common errors. Spelling and capitalization can lead to tons of errors


SyntaxError

Anything that doesn't follow the syntax provided in the Python documentation.

>>> print(Wiley, come inside')  # missing quote will give an error

SyntaxError: invalid syntax

Fix: Verify your quotes, brackets, spelling, and everything

>>> print('Wiley, come inside')
Wiley, come inside

NameError

>>> print(dog)  # dog was never defined
Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    print(dog)
NameError: name 'dog' is not defined

Fix: define a function, class, or variable dog

>>> dog = 'Wiley'
>>> print(dog)
Wiley

IndexError

>>> numbers = [1, 2, 3, 4]
>>> numbers[9]
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    numbers[9]
IndexError: list index out of range

Fix: verify you are using indices that are in your list. This list has index 0, 1, 2, and 3


ModuleNotFoundError

>>> import turtles
Traceback (most recent call last):
  File "<pyshell#16>", line 1, in <module>
    import turtles
ModuleNotFoundError: No module named 'turtles'

Fix: make sure you spell the module name correctly

import turtle

Note: if you're curious about this module, check it out in the Python docs.


AttributeError

Each module and class can have attributes.

>>> import turtle
>>> turtle.PenColor('#0B3C49')
Traceback (most recent call last):
  File "<pyshell#19>", line 1, in <module>
    turtle.PenColor
AttributeError: module 'turtle' has no attribute 'PenColor'

Fix: Make sure you're using the correct attributes. Make sure the capitalization and spelling are correct.

>>> import turtle
>>> turtle.pencolor('#0B3C49')  # that's just the hexcode for a blue color

KeyError

>>> dog = {'name':'Cheeto', 'age':4.5, 'breed':'Chihuahua'}
>>> dog['bread']
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    dog['bread']
KeyError: 'bread'

Fix: Make sure the capitalization and spelling are correct.

>>> dog['breed']
'Chihuahua'

TypeError

>>> 4 + '3.2'
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    4 + '3.2'
TypeError: unsupported operand type(s) for +: 'int' and 'str'

Fix: Make sure your data types are compatible. You cannot add strings to numbers.

>>> 4 + float('3.2')  # change/cast your string to a float
7.2

TypeErrors may also happen if you try to import part of a module when you should import the entire module. For example from time import sleep does not work. You should use import time.


ImportError

>>> from turtle import dog
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    from turtle import dog
ImportError: cannot import name 'dog' from 'turtle' (/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/turtle.py)

Fix: There is no dog to import. Review the docs and make sure you're importing the right thing.

>>> from turtle import *  # `*` means all

ValueError

>>> int('5tr1ng')
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    int('5tr1ng')
ValueError: invalid literal for int() with base 10: '5tr1ng'

Fix: The only fix here is to make sure you were doing what you meant to do. You cannot cast this alphanumeric string into a number. Though, if you meant to have a string with a number, just fix it and try again

>>> int('51')
51

ZeroDivisionError

>>> 27/0
Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    27/0
ZeroDivisionError: division by zero

Fix: The only fix here is to make sure you were doing what you meant to do. You cannot divide by 0

Important: Just in case you missed it, spelling and capitalization lead to tons of errors. Before you assume Python is broken, run spellcheck and make sure your class & variable names are right.


Series based on

Posted on by:

vickilanger profile

Vicki (she/her)

@vickilanger

Vicki was once a manager of aircraft maintenance and is now charming Python. She coded #VetsWhoCode bot & Code Questions bot. When Vicki isn't petting her cat, she’s throwing a ball for her dogs.

Discussion

pic
Editor guide