Skip to content
Snippets Groups Projects
Verified Commit 4c15a5a7 authored by Renato Alves's avatar Renato Alves :seedling:
Browse files

Initial commit - session 1 - errors and exceptions

parents
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id: tags:
See more at: https://tobyhodges.github.io/python-embl-jan-2020/19-errors/index.html
%% Cell type:code id: tags:
``` python
def favorite_ice_cream():
ice_creams = [
"chocolate",
"vanilla",
"strawberry"
]
print(ice_creams[3])
favorite_ice_cream()
```
%% Cell type:code id: tags:
``` python
def increment(i=0):
if i > 3:
oops
else:
increment(i + 1)
increment()
```
%% Cell type:markdown id: tags:
# Syntax errors
%% Cell type:code id: tags:
``` python
def some_function()
msg = "hello, world!"
print(msg)
return msg
```
%% Cell type:code id: tags:
``` python
def some_function():
msg = "hello, world!"
print(msg)
return msg
```
%% Cell type:code id: tags:
``` python
def some_function():
msg = "hello, world!"
print(msg)
return msg
```
%% Cell type:markdown id: tags:
# Variable name errors
%% Cell type:code id: tags:
``` python
print(a)
```
%% Cell type:code id: tags:
``` python
print(hello)
```
%% Cell type:code id: tags:
``` python
for number in range(10):
count = count + number
print("The count is:", count)
```
%% Cell type:code id: tags:
``` python
Count = 0
for number in range(10):
count = count + number
print("The count is:", count)
```
%% Cell type:markdown id: tags:
# Index errors
%% Cell type:code id: tags:
``` python
letters = ['a', 'b', 'c']
print("Letter #1 is", letters[0])
print("Letter #2 is", letters[1])
print("Letter #3 is", letters[2])
print("Letter #4 is", letters[3])
```
%% Cell type:markdown id: tags:
# File errors
%% Cell type:code id: tags:
``` python
file_handle = open('myfile.txt', 'r')
```
%% Cell type:code id: tags:
``` python
file_handle = open('myotherfile.txt', 'w')
file_handle.read()
```
%% Cell type:code id: tags:
``` python
import os
print("File myunreadable.txt exists:", os.path.isfile("myunreadable.txt"))
```
%% Cell type:code id: tags:
``` python
file_handle = open('myunreadable.txt', 'r')
```
%% Cell type:markdown id: tags:
# Type errors
%% Cell type:code id: tags:
``` python
num = 1
text = '2'
num + text
```
%% Cell type:code id: tags:
``` python
num + num
```
%% Cell type:code id: tags:
``` python
text + text
```
%% Cell type:code id: tags:
``` python
num - num
```
%% Cell type:code id: tags:
``` python
text - text
```
%% Cell type:code id: tags:
``` python
num * num
```
%% Cell type:code id: tags:
``` python
text * text
```
%% Cell type:markdown id: tags:
# Numeric errors
%% Cell type:code id: tags:
``` python
1 / 0
```
%% Cell type:markdown id: tags:
# Resource errors
%% Cell type:code id: tags:
``` python
list(range(10000000000000))
```
%% Cell type:markdown id: tags:
# Exception hierarchy
https://docs.python.org/3/library/exceptions.html#exception-hierarchy
# Exceptions built into python
https://docs.python.org/3/library/exceptions.html#base-classes
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment