DEV Community

Vicki Langer
Vicki Langer

Posted on • Edited on

Charming the Python: Error Types

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
Enter fullscreen mode Exit fullscreen mode

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

>>> print('Wiley, come inside')
Wiley, come inside
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Fix: define a function, class, or variable dog

>>> dog = 'Wiley'
>>> print(dog)
Wiley
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

Fix: make sure you spell the module name correctly

import turtle
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

Fix: Make sure the capitalization and spelling are correct.

>>> dog['breed']
'Chihuahua'
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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

>>> from turtle import *  # `*` means all
Enter fullscreen mode Exit fullscreen mode

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'
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

ZeroDivisionError

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

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

Top comments (0)