Charming the Python: Error Types
Tags: python, beginners
Published: 2019-12-16 22:58:17

*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](https://docs.python.org/3/reference/index.html).
```python
>>> print(Wiley, come inside')  # missing quote will give an error
      
SyntaxError: invalid syntax
```

**Fix**: Verify your quotes, brackets, spelling, and everything
```python
>>> print('Wiley, come inside')
Wiley, come inside
```
---
### NameError

```python
>>> 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`
```python
>>> dog = 'Wiley'
>>> print(dog)
Wiley
```

---
### IndexError

```python
>>> 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

```python
>>> 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
```python
import turtle
```
Note: if you're curious about this module, check it out in the [Python docs](https://docs.python.org/3/library/turtle.html).

---
### AttributeError
Each module and class can have attributes.
```python
>>> 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.
```python
>>> import turtle
>>> turtle.pencolor('#0B3C49')  # that's just the hexcode for a blue color
```

---
### KeyError

```python
>>> 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.
```python
>>> dog['breed']
'Chihuahua'
```

---
### TypeError

```python
>>> 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.
```python
>>> 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

```python
>>> 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](https://docs.python.org/3/library/turtle.html) and make sure you're importing the right thing.
```python
>>> from turtle import *  # `*` means all
```

---
### ValueError

```python
>>> 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
```python
>>> int('51')
51
```

---
### ZeroDivisionError

```python
>>> 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 {% post https://dev.to/asabeneh/30daysofpyton-challenge-43ii %}