diff --git a/01-numpy.html b/01-numpy.html index d3dca2f7d..d7491adb3 100644 --- a/01-numpy.html +++ b/01-numpy.html @@ -28,7 +28,7 @@
In order to load our inflammation data, we need to import a library called NumPy. In general you should use this library if you want to do fancy things with numbers, especially if you have matrices. We can load NumPy using:
import numpy
Importing a library is like getting a piece of lab equipment out of a storage locker and setting it up on the bench. Once it's done, we can ask the library to read our data file for us:
-numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')
+numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')
array([[ 0., 0., 1., ..., 3., 0., 0.],
[ 0., 1., 2., ..., 1., 0., 1.],
[ 0., 1., 1., ..., 2., 1., 1.],
@@ -93,7 +93,7 @@ Learning Objectives
[ 0. 0. 1. ..., 1. 1. 0.]]
Now that our data is in memory, we can start doing things with it. First, let's ask what type of thing data refers to:
print type(data)
-<type 'numpy.ndarray'>
+<type 'numpy.ndarray'>
The output tells us that data currently refers to an N-dimensional array created by the NumPy library. We can see what its shape is like this:
print data.shape
(60, 40)
diff --git a/01-numpy.md b/01-numpy.md
index e120c162e..65f317d56 100644
--- a/01-numpy.md
+++ b/01-numpy.md
@@ -36,7 +36,7 @@ Once it's done,
we can ask the library to read our data file for us:
~~~ {.python}
-numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')
+numpy.loadtxt(fname='inflammation-01.csv', delimiter=',')
~~~
~~~ {.output}
array([[ 0., 0., 1., ..., 3., 0., 0.],
@@ -189,7 +189,7 @@ let's ask what **type** of thing `data` refers to:
print type(data)
~~~
~~~ {.output}
-<type 'numpy.ndarray'>
+print 'final value of temp after all function calls:', temp
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
-<ipython-input-12-ffd9b4dbd5f1> in <module>()
-----> 1 print 'final value of temp after all function calls:', temp
+<ipython-input-12-ffd9b4dbd5f1> in <module>()
+----> 1 print 'final value of temp after all function calls:', temp
NameError: name 'temp' is not defined
final value of temp after all function calls:
@@ -195,7 +195,7 @@ A string like this is called a docstring. We don't need to use triple quotes when we write one, but if we do, we can break the string across multiple lines:
def center(data, desired):
'''Return a new array containing the original data centered around the desired value.
- Example: center([1, 2, 3], 0) => [-1, 0, 1]'''
+ Example: center([1, 2, 3], 0) => [-1, 0, 1]'''
return (data - data.mean()) + desired
help(center)
@@ -203,7 +203,7 @@ We have passed parameters to functions in two ways: directly, as in span(data), and by name, as in numpy.loadtxt(fname='something.csv', delimiter=','). In fact, we can pass the filename to loadtxt without the fname=:
numpy.loadtxt('inflammation-01.csv', ',')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
-<ipython-input-26-e3bc6cf4fd6a> in <module>()
-----> 1 numpy.loadtxt('inflammation-01.csv', ',')
+<ipython-input-26-e3bc6cf4fd6a> in <module>()
+----> 1 numpy.loadtxt('inflammation-01.csv', ',')
/Users/gwilson/anaconda/lib/python2.7/site-packages/numpy/lib/npyio.pyc in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin)
775 try:
776 # Make sure we're dealing with a proper dtype
---> 777 dtype = np.dtype(dtype)
+--> 777 dtype = np.dtype(dtype)
778 defconv = _getconv(dtype)
779
@@ -227,7 +227,7 @@ Defining Defaults
To understand what's going on, and make our own functions easier to use, let's re-define our center function like this:
def center(data, desired=0.0):
'''Return a new array containing the original data centered around the desired value (0 by default).
- Example: center([1, 2, 3], 0) => [-1, 0, 1]'''
+ Example: center([1, 2, 3], 0) => [-1, 0, 1]'''
return (data - data.mean()) + desired
The key change is that the second parameter is now written desired=0.0 instead of just desired. If we call the function with two arguments, it works as it did before:
test_data = numpy.zeros((2, 2))
@@ -267,7 +267,7 @@ Defining Defaults
help(numpy.loadtxt)
Help on function loadtxt in module numpy.lib.npyio:
-loadtxt(fname, dtype=<type 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)
+loadtxt(fname, dtype=<type 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)
Load data from a text file.
Each row in the text file must have the same number of values.
@@ -331,23 +331,23 @@ Defining Defaults
Examples
--------
- >>> from StringIO import StringIO # StringIO behaves like a file object
- >>> c = StringIO("0 1\n2 3")
- >>> np.loadtxt(c)
+ >>> from StringIO import StringIO # StringIO behaves like a file object
+ >>> c = StringIO("0 1\n2 3")
+ >>> np.loadtxt(c)
array([[ 0., 1.],
[ 2., 3.]])
- >>> d = StringIO("M 21 72\nF 35 58")
- >>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'),
+ >>> d = StringIO("M 21 72\nF 35 58")
+ >>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'),
... 'formats': ('S1', 'i4', 'f4')})
array([('M', 21, 72.0), ('F', 35, 58.0)],
- dtype=[('gender', '|S1'), ('age', '<i4'), ('weight', '<f4')])
+ dtype=[('gender', '|S1'), ('age', '<i4'), ('weight', '<f4')])
- >>> c = StringIO("1,0,2\n3,0,4")
- >>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True)
- >>> x
+ >>> c = StringIO("1,0,2\n3,0,4")
+ >>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True)
+ >>> x
array([ 1., 3.])
- >>> y
+ >>> y
array([ 2., 4.])
There's a lot of information here, but the most important part is the first couple of lines:
diff --git a/02-func.md b/02-func.md
index 7a0800cf3..24327b5a3 100644
--- a/02-func.md
+++ b/02-func.md
@@ -291,8 +291,8 @@ print 'final value of temp after all function calls:', temp
~~~ {.error}
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
-<ipython-input-12-ffd9b4dbd5f1> in <module>()
-----> 1 print 'final value of temp after all function calls:', temp
+ in ()
+----> 1 print 'final value of temp after all function calls:', temp
NameError: name 'temp' is not defined
~~~
@@ -478,7 +478,7 @@ we can break the string across multiple lines:
~~~ {.python}
def center(data, desired):
'''Return a new array containing the original data centered around the desired value.
- Example: center([1, 2, 3], 0) => [-1, 0, 1]'''
+ Example: center([1, 2, 3], 0) => [-1, 0, 1]'''
return (data - data.mean()) + desired
help(center)
@@ -488,7 +488,7 @@ Help on function center in module __main__:
center(data, desired)
Return a new array containing the original data centered around the desired value.
- Example: center([1, 2, 3], 0) => [-1, 0, 1]
+ Example: center([1, 2, 3], 0) => [-1, 0, 1]
~~~
@@ -520,13 +520,13 @@ numpy.loadtxt('inflammation-01.csv', ',')
~~~ {.error}
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
-<ipython-input-26-e3bc6cf4fd6a> in <module>()
-----> 1 numpy.loadtxt('inflammation-01.csv', ',')
+ in ()
+----> 1 numpy.loadtxt('inflammation-01.csv', ',')
/Users/gwilson/anaconda/lib/python2.7/site-packages/numpy/lib/npyio.pyc in loadtxt(fname, dtype, comments, delimiter, converters, skiprows, usecols, unpack, ndmin)
775 try:
776 # Make sure we're dealing with a proper dtype
---> 777 dtype = np.dtype(dtype)
+--> 777 dtype = np.dtype(dtype)
778 defconv = _getconv(dtype)
779
@@ -540,7 +540,7 @@ let's re-define our `center` function like this:
~~~ {.python}
def center(data, desired=0.0):
'''Return a new array containing the original data centered around the desired value (0 by default).
- Example: center([1, 2, 3], 0) => [-1, 0, 1]'''
+ Example: center([1, 2, 3], 0) => [-1, 0, 1]'''
return (data - data.mean()) + desired
~~~
@@ -622,7 +622,7 @@ help(numpy.loadtxt)
~~~ {.output}
Help on function loadtxt in module numpy.lib.npyio:
-loadtxt(fname, dtype=<type 'float'>, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)
+loadtxt(fname, dtype=, comments='#', delimiter=None, converters=None, skiprows=0, usecols=None, unpack=False, ndmin=0)
Load data from a text file.
Each row in the text file must have the same number of values.
@@ -686,23 +686,23 @@ loadtxt(fname, dtype=<type 'float'>, comments='#', delimiter=None, convert
Examples
--------
- >>> from StringIO import StringIO # StringIO behaves like a file object
- >>> c = StringIO("0 1\n2 3")
- >>> np.loadtxt(c)
+ >>> from StringIO import StringIO # StringIO behaves like a file object
+ >>> c = StringIO("0 1\n2 3")
+ >>> np.loadtxt(c)
array([[ 0., 1.],
[ 2., 3.]])
- >>> d = StringIO("M 21 72\nF 35 58")
- >>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'),
+ >>> d = StringIO("M 21 72\nF 35 58")
+ >>> np.loadtxt(d, dtype={'names': ('gender', 'age', 'weight'),
... 'formats': ('S1', 'i4', 'f4')})
array([('M', 21, 72.0), ('F', 35, 58.0)],
- dtype=[('gender', '|S1'), ('age', '<i4'), ('weight', '<f4')])
+ dtype=[('gender', '|S1'), ('age', '>> c = StringIO("1,0,2\n3,0,4")
+ >>> x, y = np.loadtxt(c, delimiter=',', usecols=(0, 2), unpack=True)
+ >>> x
array([ 1., 3.])
- >>> y
+ >>> y
array([ 2., 4.])
~~~
diff --git a/03-loop.html b/03-loop.html
index c2f1e19dd..67511711e 100644
--- a/03-loop.html
+++ b/03-loop.html
@@ -90,8 +90,8 @@ For Loops
print_characters('tin')
~
-IndexError Traceback (most recent call last) <ipython-input-13-5bc7311e0bf3> in <module>() ----> 1 print_characters('tin')
-<ipython-input-12-11460561ea56> in print_characters(element) 3 print element[1] 4 print element[2] ----> 5 print element[3] 6 7 print_characters('lead')
+IndexError Traceback (most recent call last) in () ----> 1 print_characters('tin')
+ in print_characters(element) 3 print element[1] 4 print element[2] ----> 5 print element[3] 6 7 print_characters('lead')
IndexError: string index out of range ~ {.output} t i n ~
Here's a better approach:
def print_characters(element):
diff --git a/03-loop.md b/03-loop.md
index 8d505818e..81eb28519 100644
--- a/03-loop.md
+++ b/03-loop.md
@@ -100,13 +100,13 @@ print_characters('tin')
~~~ {.error}
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
-<ipython-input-13-5bc7311e0bf3> in <module>()
-----> 1 print_characters('tin')
+ in ()
+----> 1 print_characters('tin')
-<ipython-input-12-11460561ea56> in print_characters(element)
+ in print_characters(element)
3 print element[1]
4 print element[2]
-----> 5 print element[3]
+----> 5 print element[3]
6
7 print_characters('lead')
diff --git a/04-cond.html b/04-cond.html
index 44061f44f..852f093f8 100644
--- a/04-cond.html
+++ b/04-cond.html
@@ -72,16 +72,16 @@ Image Grids
print 'first element of color after change:', color[0]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
-<ipython-input-11-9c3dd30a4e52> in <module>()
-----> 1 color[0] = 40
+<ipython-input-11-9c3dd30a4e52> in <module>()
+----> 1 color[0] = 40
2 print 'first element of color after change:', color[0]
TypeError: 'tuple' object does not support item assignment
If a tuple represents an RGB color, its red, green, and blue components can take on values between 0 and 255. The upper bound may seem odd, but it's the largest number that can be represented in an 8-bit byte (i.e., 28-1). This makes it easy for computers to manipulate colors, while providing fine enough gradations to fool most human eyes, most of the time.
Let's see what a few RGB colors actually look like:
row = ImageGrid(8, 1)
-row[0, 0] = (0, 0, 0) # no color => black
-row[1, 0] = (255, 255, 255) # all colors => white
+row[0, 0] = (0, 0, 0) # no color => black
+row[1, 0] = (255, 255, 255) # all colors => white
row[2, 0] = (255, 0, 0) # all red
row[3, 0] = (0, 255, 0) # all green
row[4, 0] = (0, 0, 255) # all blue
@@ -108,7 +108,7 @@ Image Grids
Conditionals
The other thing we need in order to create a heat map of our own is a way to pick a color based on a data value. The tool Python gives us for doing this is called a conditional statement, and looks like this:
num = 37
-if num > 100:
+if num > 100:
print 'greater'
else:
print 'not greater'
@@ -121,14 +121,14 @@ Conditionals
Conditional statements don't have to include an else. If there isn't one, Python simply does nothing if the test is false:
num = 53
print 'before conditional...'
-if num > 100:
+if num > 100:
print '53 is greater than 100'
print '...after conditional'
before conditional...
...after conditional
We can also chain several tests together using elif, which is short for "else if". This makes it simple to write a function that returns the sign of a number:
def sign(num):
- if num > 0:
+ if num > 0:
return 1
elif num == 0:
return 0
@@ -139,13 +139,13 @@ Conditionals
sign of -3: -1
One important thing to notice in the code above is that we use a double equals sign == to test for equality rather than a single equals sign because the latter is used to mean assignment. This convention was inherited from C, and while many other programming languages work the same way, it does take a bit of getting used to...
We can also combine tests using and and or. and is only true if both parts are true:
-if (1 > 0) and (-1 > 0):
+if (1 > 0) and (-1 > 0):
print 'both parts are true'
else:
print 'one part is not true'
one part is not true
while or is true if either part is true:
-if (1 < 0) or ('left' < 'right'):
+if (1 < 0) or ('left' < 'right'):
print 'at least one test is true'
at least one test is true
In this case, "either" means "either or both", not "either one or the other but not both".
@@ -154,7 +154,7 @@ Nesting
numbers = [-5, 3, 2, -1, 9, 6]
total = 0
for n in numbers:
- if n >= 0:
+ if n >= 0:
total = total + n
print 'sum of positive values:', total
sum of positive values: 20
@@ -163,7 +163,7 @@ Nesting
pos_total = 0
neg_total = 0
for n in numbers:
- if n >= 0:
+ if n >= 0:
pos_total = pos_total + n
else:
neg_total = neg_total + n
@@ -185,7 +185,7 @@ Nesting
square = ImageGrid(5, 5)
for x in range(square.width):
for y in range(square.height):
- if x < y:
+ if x < y:
square[x, y] = colors['Fuchsia']
elif x == y:
square[x, y] = colors['Olive']
@@ -207,7 +207,7 @@ Creating a Heat Map
The third step is to decide how we are going to color the cells in the heat map. To keep things simple, we will use red, green, and blue as our colors, and compare data values to the data set's mean. Here's the code:
for x in range(width):
for y in range(height):
- if data[x, y] < data.mean():
+ if data[x, y] < data.mean():
heatmap[x, y] = colors['Red']
elif data[x, y] == data.mean():
heatmap[x, y] = colors['Green']
@@ -238,9 +238,9 @@ Creating a Heat Map
center = flipped.mean()
for x in range(width):
for y in range(height):
- if flipped[x, y] < (0.8 * center):
+ if flipped[x, y] < (0.8 * center):
heatmap[x, y] = colors['Orchid']
- elif flipped[x, y] > (1.2 * center):
+ elif flipped[x, y] > (1.2 * center):
heatmap[x, y] = colors['HotPink']
else:
heatmap[x, y] = colors['Fuchsia']
@@ -255,9 +255,9 @@ Creating a Heat Map
center = values.mean()
for x in range(width):
for y in range(height):
- if values[x, y] < low_band * center:
+ if values[x, y] < low_band * center:
result[x, y] = low_color
- elif values[x, y] > high_band * center:
+ elif values[x, y] > high_band * center:
result[x, y] = high_color
else:
result[x, y] = mid_color
@@ -282,9 +282,9 @@ Creating a Heat Map
center = values.mean()
for x in range(width):
for y in range(height):
- if values[x, y] < low_band * center:
+ if values[x, y] < low_band * center:
result[x, y] = low_color
- elif values[x, y] > high_band * center:
+ elif values[x, y] > high_band * center:
result[x, y] = high_color
else:
result[x, y] = mid_color
diff --git a/04-cond.md b/04-cond.md
index a050e7e0e..b2b5ccdba 100644
--- a/04-cond.md
+++ b/04-cond.md
@@ -116,8 +116,8 @@ print 'first element of color after change:', color[0]
~~~ {.error}
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
-<ipython-input-11-9c3dd30a4e52> in <module>()
-----> 1 color[0] = 40
+ in ()
+----> 1 color[0] = 40
2 print 'first element of color after change:', color[0]
TypeError: 'tuple' object does not support item assignment
@@ -136,8 +136,8 @@ Let's see what a few RGB colors actually look like:
~~~ {.python}
row = ImageGrid(8, 1)
-row[0, 0] = (0, 0, 0) # no color => black
-row[1, 0] = (255, 255, 255) # all colors => white
+row[0, 0] = (0, 0, 0) # no color => black
+row[1, 0] = (255, 255, 255) # all colors => white
row[2, 0] = (255, 0, 0) # all red
row[3, 0] = (0, 255, 0) # all green
row[4, 0] = (0, 0, 255) # all blue
@@ -186,7 +186,7 @@ and looks like this:
~~~ {.python}
num = 37
-if num > 100:
+if num > 100:
print 'greater'
else:
print 'not greater'
@@ -215,7 +215,7 @@ Python simply does nothing if the test is false:
~~~ {.python}
num = 53
print 'before conditional...'
-if num > 100:
+if num > 100:
print '53 is greater than 100'
print '...after conditional'
~~~
@@ -230,7 +230,7 @@ This makes it simple to write a function that returns the sign of a number:
~~~ {.python}
def sign(num):
- if num > 0:
+ if num > 0:
return 1
elif num == 0:
return 0
@@ -254,7 +254,7 @@ We can also combine tests using `and` and `or`.
`and` is only true if both parts are true:
~~~ {.python}
-if (1 > 0) and (-1 > 0):
+if (1 > 0) and (-1 > 0):
print 'both parts are true'
else:
print 'one part is not true'
@@ -266,7 +266,7 @@ one part is not true
while `or` is true if either part is true:
~~~ {.python}
-if (1 < 0) or ('left' < 'right'):
+if (1 < 0) or ('left' < 'right'):
print 'at least one test is true'
~~~
~~~ {.output}
@@ -288,7 +288,7 @@ we can write this:
numbers = [-5, 3, 2, -1, 9, 6]
total = 0
for n in numbers:
- if n >= 0:
+ if n >= 0:
total = total + n
print 'sum of positive values:', total
~~~
@@ -303,7 +303,7 @@ We could equally well calculate the positive and negative sums in a single loop:
pos_total = 0
neg_total = 0
for n in numbers:
- if n >= 0:
+ if n >= 0:
pos_total = pos_total + n
else:
neg_total = neg_total + n
@@ -341,7 +341,7 @@ We can combine nesting and conditionals to create patterns in an image:
square = ImageGrid(5, 5)
for x in range(square.width):
for y in range(square.height):
- if x < y:
+ if x < y:
square[x, y] = colors['Fuchsia']
elif x == y:
square[x, y] = colors['Olive']
@@ -390,7 +390,7 @@ Here's the code:
~~~ {.python}
for x in range(width):
for y in range(height):
- if data[x, y] < data.mean():
+ if data[x, y] < data.mean():
heatmap[x, y] = colors['Red']
elif data[x, y] == data.mean():
heatmap[x, y] = colors['Green']
@@ -428,9 +428,9 @@ heatmap = ImageGrid(width, height, block_size=5)
center = flipped.mean()
for x in range(width):
for y in range(height):
- if flipped[x, y] < (0.8 * center):
+ if flipped[x, y] < (0.8 * center):
heatmap[x, y] = colors['Orchid']
- elif flipped[x, y] > (1.2 * center):
+ elif flipped[x, y] > (1.2 * center):
heatmap[x, y] = colors['HotPink']
else:
heatmap[x, y] = colors['Fuchsia']
@@ -455,9 +455,9 @@ def make_heatmap(values, low_color, mid_color, high_color, low_band, high_band,
center = values.mean()
for x in range(width):
for y in range(height):
- if values[x, y] < low_band * center:
+ if values[x, y] < low_band * center:
result[x, y] = low_color
- elif values[x, y] > high_band * center:
+ elif values[x, y] > high_band * center:
result[x, y] = high_color
else:
result[x, y] = mid_color
@@ -504,9 +504,9 @@ def make_heatmap(values,
center = values.mean()
for x in range(width):
for y in range(height):
- if values[x, y] < low_band * center:
+ if values[x, y] < low_band * center:
result[x, y] = low_color
- elif values[x, y] > high_band * center:
+ elif values[x, y] > high_band * center:
result[x, y] = high_color
else:
result[x, y] = mid_color
diff --git a/05-defensive.html b/05-defensive.html
index 03f22e03e..522390a3e 100644
--- a/05-defensive.html
+++ b/05-defensive.html
@@ -28,7 +28,7 @@
Programming with Python
Defensive Programming
-Learning Objectives
+Learning Objectives
- Explain what an assertion is.
- Add assertions to programs that correctly check the program's state.
@@ -51,15 +51,15 @@ Assertions
numbers = [1.5, 2.3, 0.7, -0.001, 4.4]
total = 0.0
for n in numbers:
- assert n >= 0.0, 'Data should only contain positive values'
+ assert n >= 0.0, 'Data should only contain positive values'
total += n
print 'total is:', total
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
-<ipython-input-19-33d87ea29ae4> in <module>()
+<ipython-input-19-33d87ea29ae4> in <module>()
2 total = 0.0
3 for n in numbers:
-----> 4 assert n >= 0.0, 'Data should only contain positive values'
+----> 4 assert n >= 0.0, 'Data should only contain positive values'
5 total += n
6 print 'total is:', total
@@ -75,48 +75,48 @@ Assertions
'''Normalizes a rectangle so that it is at the origin and 1.0 units long on its longest axis.'''
assert len(rect) == 4, 'Rectangles must contain 4 coordinates'
x0, y0, x1, y1 = rect
- assert x0 < x1, 'Invalid X coordinates'
- assert y0 < y1, 'Invalid Y coordinates'
+ assert x0 < x1, 'Invalid X coordinates'
+ assert y0 < y1, 'Invalid Y coordinates'
dx = x1 - x0
dy = y1 - y0
- if dx > dy:
+ if dx > dy:
scaled = float(dx) / dy
upper_x, upper_y = 1.0, scaled
else:
scaled = float(dx) / dy
upper_x, upper_y = scaled, 1.0
- assert 0 < upper_x <= 1.0, 'Calculated upper X coordinate invalid'
- assert 0 < upper_y <= 1.0, 'Calculated upper Y coordinate invalid'
+ assert 0 < upper_x <= 1.0, 'Calculated upper X coordinate invalid'
+ assert 0 < upper_y <= 1.0, 'Calculated upper Y coordinate invalid'
return (0, 0, upper_x, upper_y)
The preconditions on lines 2, 4, and 5 catch invalid inputs:
print normalize_rectangle( (0.0, 1.0, 2.0) ) # missing the fourth coordinate
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
-<ipython-input-21-3a97b1dcab70> in <module>()
-----> 1 print normalize_rectangle( (0.0, 1.0, 2.0) ) # missing the fourth coordinate
+<ipython-input-21-3a97b1dcab70> in <module>()
+----> 1 print normalize_rectangle( (0.0, 1.0, 2.0) ) # missing the fourth coordinate
-<ipython-input-20-408dc39f3915> in normalize_rectangle(rect)
+<ipython-input-20-408dc39f3915> in normalize_rectangle(rect)
1 def normalize_rectangle(rect):
2 '''Normalizes a rectangle so that it is at the origin and 1.0 units long on its longest axis.'''
-----> 3 assert len(rect) == 4, 'Rectangles must contain 4 coordinates'
+----> 3 assert len(rect) == 4, 'Rectangles must contain 4 coordinates'
4 x0, y0, x1, y1 = rect
- 5 assert x0 < x1, 'Invalid X coordinates'
+ 5 assert x0 < x1, 'Invalid X coordinates'
AssertionError: Rectangles must contain 4 coordinates
print normalize_rectangle( (4.0, 2.0, 1.0, 5.0) ) # X axis inverted
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
-<ipython-input-22-f05ae7878a45> in <module>()
-----> 1 print normalize_rectangle( (4.0, 2.0, 1.0, 5.0) ) # X axis inverted
+<ipython-input-22-f05ae7878a45> in <module>()
+----> 1 print normalize_rectangle( (4.0, 2.0, 1.0, 5.0) ) # X axis inverted
-<ipython-input-20-408dc39f3915> in normalize_rectangle(rect)
+<ipython-input-20-408dc39f3915> in normalize_rectangle(rect)
3 assert len(rect) == 4, 'Rectangles must contain 4 coordinates'
4 x0, y0, x1, y1 = rect
-----> 5 assert x0 < x1, 'Invalid X coordinates'
- 6 assert y0 < y1, 'Invalid Y coordinates'
+----> 5 assert x0 < x1, 'Invalid X coordinates'
+ 6 assert y0 < y1, 'Invalid Y coordinates'
7
AssertionError: Invalid X coordinates
@@ -127,13 +127,13 @@ Assertions
print normalize_rectangle( (0.0, 0.0, 5.0, 1.0) )
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
-<ipython-input-24-5f0ef7954aeb> in <module>()
-----> 1 print normalize_rectangle( (0.0, 0.0, 5.0, 1.0) )
+<ipython-input-24-5f0ef7954aeb> in <module>()
+----> 1 print normalize_rectangle( (0.0, 0.0, 5.0, 1.0) )
-<ipython-input-20-408dc39f3915> in normalize_rectangle(rect)
+<ipython-input-20-408dc39f3915> in normalize_rectangle(rect)
16
- 17 assert 0 < upper_x <= 1.0, 'Calculated upper X coordinate invalid'
----> 18 assert 0 < upper_y <= 1.0, 'Calculated upper Y coordinate invalid'
+ 17 assert 0 < upper_x <= 1.0, 'Calculated upper X coordinate invalid'
+---> 18 assert 0 < upper_y <= 1.0, 'Calculated upper Y coordinate invalid'
19
20 return (0, 0, upper_x, upper_y)
@@ -168,9 +168,9 @@ Test-Driven Development
assert range_overlap([ (0.0, 1.0), (0.0, 2.0), (-1.0, 1.0) ]) == (0.0, 1.0)
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
-<ipython-input-25-d8be150fbef6> in <module>()
+<ipython-input-25-d8be150fbef6> in <module>()
1 assert range_overlap([ (0.0, 1.0) ]) == (0.0, 1.0)
-----> 2 assert range_overlap([ (2.0, 3.0), (2.0, 4.0) ]) == (2.0, 3.0)
+----> 2 assert range_overlap([ (2.0, 3.0), (2.0, 4.0) ]) == (2.0, 3.0)
3 assert range_overlap([ (0.0, 1.0), (0.0, 2.0), (-1.0, 1.0) ]) == (0.0, 1.0)
AssertionError:
@@ -192,8 +192,8 @@ Test-Driven Development
assert range_overlap([ (0.0, 1.0), (1.0, 2.0) ]) == None
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
-<ipython-input-26-d877ef460ba2> in <module>()
-----> 1 assert range_overlap([ (0.0, 1.0), (5.0, 6.0) ]) == None
+<ipython-input-26-d877ef460ba2> in <module>()
+----> 1 assert range_overlap([ (0.0, 1.0), (5.0, 6.0) ]) == None
2 assert range_overlap([ (0.0, 1.0), (1.0, 2.0) ]) == None
AssertionError:
@@ -217,12 +217,12 @@ Test-Driven Development
test_range_overlap()
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
-<ipython-input-29-cf9215c96457> in <module>()
-----> 1 test_range_overlap()
+<ipython-input-29-cf9215c96457> in <module>()
+----> 1 test_range_overlap()
-<ipython-input-28-5d4cd6fd41d9> in test_range_overlap()
+<ipython-input-28-5d4cd6fd41d9> in test_range_overlap()
1 def test_range_overlap():
-----> 2 assert range_overlap([ (0.0, 1.0), (5.0, 6.0) ]) == None
+----> 2 assert range_overlap([ (0.0, 1.0), (5.0, 6.0) ]) == None
3 assert range_overlap([ (0.0, 1.0), (1.0, 2.0) ]) == None
4 assert range_overlap([ (0.0, 1.0) ]) == (0.0, 1.0)
5 assert range_overlap([ (2.0, 3.0), (2.0, 4.0) ]) == (2.0, 3.0)
diff --git a/05-defensive.md b/05-defensive.md
index 348bc1c51..86b693799 100644
--- a/05-defensive.md
+++ b/05-defensive.md
@@ -61,17 +61,17 @@ this piece of code halts as soon as the loop encounters a value that isn't posit
numbers = [1.5, 2.3, 0.7, -0.001, 4.4]
total = 0.0
for n in numbers:
- assert n >= 0.0, 'Data should only contain positive values'
+ assert n >= 0.0, 'Data should only contain positive values'
total += n
print 'total is:', total
~~~
~~~ {.error}
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
-<ipython-input-19-33d87ea29ae4> in <module>()
+ in ()
2 total = 0.0
3 for n in numbers:
-----> 4 assert n >= 0.0, 'Data should only contain positive values'
+----> 4 assert n >= 0.0, 'Data should only contain positive values'
5 total += n
6 print 'total is:', total
@@ -103,20 +103,20 @@ def normalize_rectangle(rect):
'''Normalizes a rectangle so that it is at the origin and 1.0 units long on its longest axis.'''
assert len(rect) == 4, 'Rectangles must contain 4 coordinates'
x0, y0, x1, y1 = rect
- assert x0 < x1, 'Invalid X coordinates'
- assert y0 < y1, 'Invalid Y coordinates'
+ assert x0 < x1, 'Invalid X coordinates'
+ assert y0 < y1, 'Invalid Y coordinates'
dx = x1 - x0
dy = y1 - y0
- if dx > dy:
+ if dx > dy:
scaled = float(dx) / dy
upper_x, upper_y = 1.0, scaled
else:
scaled = float(dx) / dy
upper_x, upper_y = scaled, 1.0
- assert 0 < upper_x <= 1.0, 'Calculated upper X coordinate invalid'
- assert 0 < upper_y <= 1.0, 'Calculated upper Y coordinate invalid'
+ assert 0 < upper_x <= 1.0, 'Calculated upper X coordinate invalid'
+ assert 0 < upper_y <= 1.0, 'Calculated upper Y coordinate invalid'
return (0, 0, upper_x, upper_y)
~~~
@@ -129,15 +129,15 @@ print normalize_rectangle( (0.0, 1.0, 2.0) ) # missing the fourth coordinate
~~~ {.error}
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
-<ipython-input-21-3a97b1dcab70> in <module>()
-----> 1 print normalize_rectangle( (0.0, 1.0, 2.0) ) # missing the fourth coordinate
+ in ()
+----> 1 print normalize_rectangle( (0.0, 1.0, 2.0) ) # missing the fourth coordinate
-<ipython-input-20-408dc39f3915> in normalize_rectangle(rect)
+ in normalize_rectangle(rect)
1 def normalize_rectangle(rect):
2 '''Normalizes a rectangle so that it is at the origin and 1.0 units long on its longest axis.'''
-----> 3 assert len(rect) == 4, 'Rectangles must contain 4 coordinates'
+----> 3 assert len(rect) == 4, 'Rectangles must contain 4 coordinates'
4 x0, y0, x1, y1 = rect
- 5 assert x0 < x1, 'Invalid X coordinates'
+ 5 assert x0 < x1, 'Invalid X coordinates'
AssertionError: Rectangles must contain 4 coordinates
~~~
@@ -148,14 +148,14 @@ print normalize_rectangle( (4.0, 2.0, 1.0, 5.0) ) # X axis inverted
~~~ {.error}
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
-<ipython-input-22-f05ae7878a45> in <module>()
-----> 1 print normalize_rectangle( (4.0, 2.0, 1.0, 5.0) ) # X axis inverted
+ in ()
+----> 1 print normalize_rectangle( (4.0, 2.0, 1.0, 5.0) ) # X axis inverted
-<ipython-input-20-408dc39f3915> in normalize_rectangle(rect)
+ in normalize_rectangle(rect)
3 assert len(rect) == 4, 'Rectangles must contain 4 coordinates'
4 x0, y0, x1, y1 = rect
-----> 5 assert x0 < x1, 'Invalid X coordinates'
- 6 assert y0 < y1, 'Invalid Y coordinates'
+----> 5 assert x0 < x1, 'Invalid X coordinates'
+ 6 assert y0 < y1, 'Invalid Y coordinates'
7
AssertionError: Invalid X coordinates
@@ -181,13 +181,13 @@ print normalize_rectangle( (0.0, 0.0, 5.0, 1.0) )
~~~ {.error}
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
-<ipython-input-24-5f0ef7954aeb> in <module>()
-----> 1 print normalize_rectangle( (0.0, 0.0, 5.0, 1.0) )
+ in ()
+----> 1 print normalize_rectangle( (0.0, 0.0, 5.0, 1.0) )
-<ipython-input-20-408dc39f3915> in normalize_rectangle(rect)
+ in normalize_rectangle(rect)
16
- 17 assert 0 < upper_x <= 1.0, 'Calculated upper X coordinate invalid'
----> 18 assert 0 < upper_y <= 1.0, 'Calculated upper Y coordinate invalid'
+ 17 assert 0 < upper_x <= 1.0, 'Calculated upper X coordinate invalid'
+---> 18 assert 0 < upper_y <= 1.0, 'Calculated upper Y coordinate invalid'
19
20 return (0, 0, upper_x, upper_y)
@@ -275,9 +275,9 @@ assert range_overlap([ (0.0, 1.0), (0.0, 2.0), (-1.0, 1.0) ]) == (0.0, 1.0)
~~~ {.error}
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
-<ipython-input-25-d8be150fbef6> in <module>()
+ in ()
1 assert range_overlap([ (0.0, 1.0) ]) == (0.0, 1.0)
-----> 2 assert range_overlap([ (2.0, 3.0), (2.0, 4.0) ]) == (2.0, 3.0)
+----> 2 assert range_overlap([ (2.0, 3.0), (2.0, 4.0) ]) == (2.0, 3.0)
3 assert range_overlap([ (0.0, 1.0), (0.0, 2.0), (-1.0, 1.0) ]) == (0.0, 1.0)
AssertionError:
@@ -344,8 +344,8 @@ assert range_overlap([ (0.0, 1.0), (1.0, 2.0) ]) == None
~~~ {.error}
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
-<ipython-input-26-d877ef460ba2> in <module>()
-----> 1 assert range_overlap([ (0.0, 1.0), (5.0, 6.0) ]) == None
+ in ()
+----> 1 assert range_overlap([ (0.0, 1.0), (5.0, 6.0) ]) == None
2 assert range_overlap([ (0.0, 1.0), (1.0, 2.0) ]) == None
AssertionError:
@@ -390,12 +390,12 @@ test_range_overlap()
~~~ {.error}
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
-<ipython-input-29-cf9215c96457> in <module>()
-----> 1 test_range_overlap()
+ in ()
+----> 1 test_range_overlap()
-<ipython-input-28-5d4cd6fd41d9> in test_range_overlap()
+ in test_range_overlap()
1 def test_range_overlap():
-----> 2 assert range_overlap([ (0.0, 1.0), (5.0, 6.0) ]) == None
+----> 2 assert range_overlap([ (0.0, 1.0), (5.0, 6.0) ]) == None
3 assert range_overlap([ (0.0, 1.0), (1.0, 2.0) ]) == None
4 assert range_overlap([ (0.0, 1.0) ]) == (0.0, 1.0)
5 assert range_overlap([ (2.0, 3.0), (2.0, 4.0) ]) == (2.0, 3.0)
diff --git a/06-cmdline.html b/06-cmdline.html
index 48c6f2d86..fb0db86c1 100644
--- a/06-cmdline.html
+++ b/06-cmdline.html
@@ -276,7 +276,7 @@ Handling Standard Input
print count, 'lines in standard input'
This little program reads lines from a special "file" called sys.stdin, which is automatically connected to the program's standard input. We don't have to open it --- Python and the operating system take care of that when the program starts up --- but we can do almost anything with it that we could do to a regular file. Let's try running it as if it were a regular command-line program:
-$ python count-stdin.py < small-01.csv
+$ python count-stdin.py < small-01.csv
2 lines in standard input
A common mistake is to try to run something that reads from standard input like this:
$ count_stdin.py small-01.csv
diff --git a/06-cmdline.md b/06-cmdline.md
index f0a53821c..416949d49 100644
--- a/06-cmdline.md
+++ b/06-cmdline.md
@@ -463,7 +463,7 @@ but we can do almost anything with it that we could do to a regular file.
Let's try running it as if it were a regular command-line program:
~~~ {.input}
-$ python count-stdin.py < small-01.csv
+$ python count-stdin.py < small-01.csv
~~~
~~~ {.output}
2 lines in standard input
diff --git a/07-errors.html b/07-errors.html
index 3ace53d43..0398462e8 100644
--- a/07-errors.html
+++ b/07-errors.html
@@ -51,14 +51,14 @@ Learning Objectives
favorite_ice_cream()
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
-<ipython-input-1-9d0462a5b07c> in <module>()
+<ipython-input-1-9d0462a5b07c> in <module>()
1 from errors_01 import favorite_ice_cream
-----> 2 favorite_ice_cream()
+----> 2 favorite_ice_cream()
/Users/jhamrick/project/swc/novice/python/errors_01.pyc in favorite_ice_cream()
5 "strawberry"
6 ]
-----> 7 print ice_creams[3]
+----> 7 print ice_creams[3]
IndexError: list index out of range
This particular traceback has two levels. You can determine the number of levels by looking for the number of arrows on the left hand side. In this case:
@@ -84,7 +84,7 @@ Syntax Errors
msg = "hello, world!"
print msg
return msg
- File "<ipython-input-3-6bb841ea1423>", line 1
+ File "<ipython-input-3-6bb841ea1423>", line 1
def some_function()
^
SyntaxError: invalid syntax
@@ -94,7 +94,7 @@ Syntax Errors
msg = "hello, world!"
print msg
return msg
- File "<ipython-input-4-ae290e7659cb>", line 4
+ File "<ipython-input-4-ae290e7659cb>", line 4
return msg
^
IndentationError: unexpected indent
@@ -106,7 +106,7 @@ Tabs and Spaces
msg = "hello, world!"
print msg
return msg
- File "<ipython-input-5-653b36fbcd41>", line 4
+ File "<ipython-input-5-653b36fbcd41>", line 4
return msg
^
IndentationError: unindent does not match any outer indentation level
@@ -117,8 +117,8 @@ Variable Name Errors
print a
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
-<ipython-input-7-9d7b17ad5387> in <module>()
-----> 1 print a
+<ipython-input-7-9d7b17ad5387> in <module>()
+----> 1 print a
NameError: name 'a' is not defined
Variable name errors come with some of the most informative error messages, which are usually of the form "name 'the_variable_name' is not defined".
@@ -126,8 +126,8 @@ Variable Name Errors
print hello
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
-<ipython-input-8-9553ee03b645> in <module>()
-----> 1 print hello
+<ipython-input-8-9553ee03b645> in <module>()
+----> 1 print hello
NameError: name 'hello' is not defined
The second is that you just forgot to create the variable before using it. In the following example, count should have been defined (e.g., with count = 0) before the for loop:
@@ -136,9 +136,9 @@ Variable Name Errors
print "The count is: " + str(count)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
-<ipython-input-9-dd6a12d7ca5c> in <module>()
+<ipython-input-9-dd6a12d7ca5c> in <module>()
1 for number in range(10):
-----> 2 count = count + number
+----> 2 count = count + number
3 print "The count is: " + str(count)
NameError: name 'count' is not defined
@@ -149,10 +149,10 @@ Variable Name Errors
print "The count is: " + str(count)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
-<ipython-input-10-d77d40059aea> in <module>()
+<ipython-input-10-d77d40059aea> in <module>()
1 Count = 0
2 for number in range(10):
-----> 3 count = count + number
+----> 3 count = count + number
4 print "The count is: " + str(count)
NameError: name 'count' is not defined
@@ -168,10 +168,10 @@ Item Errors
Letter #3 is c
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
-<ipython-input-11-d817f55b7d6c> in <module>()
+<ipython-input-11-d817f55b7d6c> in <module>()
3 print "Letter #2 is " + letters[1]
4 print "Letter #3 is " + letters[2]
-----> 5 print "Letter #4 is " + letters[3]
+----> 5 print "Letter #4 is " + letters[3]
IndexError: list index out of range
Here, Python is telling us that there is an IndexError in our code, meaning we tried to access a list index that did not exist. We get a similar error in the case of dictionaries:
@@ -185,10 +185,10 @@ Item Errors
print "The capital of Oregon is: " + us_state_capitals['oregon']
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
-<ipython-input-12-27fa113dd73c> in <module>()
+<ipython-input-12-27fa113dd73c> in <module>()
6 }
7
-----> 8 print "The capital of Oregon is: " + us_state_capitals['oregon']
+----> 8 print "The capital of Oregon is: " + us_state_capitals['oregon']
KeyError: 'oregon'
In this case, we get a KeyError, which means that the key we requested ('oregon', as the error message tells us) is not present in the dictionary. This might be because it genuinely does not exist in the dictionary, but it could also be due to a typo. This is similar to the case we discussed above, where you can sometimes receive a NameError due to a typo. For example:
@@ -202,10 +202,10 @@ Item Errors
print "The capital of Massachusetts is: " + us_state_capitals['massachussetts']
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
-<ipython-input-13-ae1dac4c6a45> in <module>()
+<ipython-input-13-ae1dac4c6a45> in <module>()
6 }
7
-----> 8 print "The capital of Massachusetts is: " + us_state_capitals['massachussetts']
+----> 8 print "The capital of Massachusetts is: " + us_state_capitals['massachussetts']
KeyError: 'massachussetts'
File Errors
@@ -213,8 +213,8 @@ File Errors
file_handle = open('myfile.txt', 'r')
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
-<ipython-input-14-f6e1ac4aee96> in <module>()
-----> 1 file_handle = open('myfile.txt', 'r')
+<ipython-input-14-f6e1ac4aee96> in <module>()
+----> 1 file_handle = open('myfile.txt', 'r')
IOError: [Errno 2] No such file or directory: 'myfile.txt'
One reason for receiving this error is that you specified an incorrect path to the file. For example, if I am currently in a folder called myproject, and I have a file in myproject/writing/myfile.txt, but I try to just open myfile.txt, this will fail. The correct path would be writing/myfile. xt. It is also possible (like with NameError and KeyError) that you just made a typo.
@@ -223,9 +223,9 @@ File Errors
file_handle.read()
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
-<ipython-input-15-b846479bc61f> in <module>()
+<ipython-input-15-b846479bc61f> in <module>()
1 file_handle = open('myfile.txt', 'w')
-----> 2 file_handle.read()
+----> 2 file_handle.read()
IOError: File not open for reading
@@ -243,19 +243,19 @@ Reading Error Messages
print_friday_message()
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
-<ipython-input-2-e4c4cbafeeb5> in <module>()
+<ipython-input-2-e4c4cbafeeb5> in <module>()
1 from errors_02 import print_friday_message
-----> 2 print_friday_message()
+----> 2 print_friday_message()
/Users/jhamrick/project/swc/novice/python/errors_02.py in print_friday_message()
13
14 def print_friday_message():
----> 15 print_message("Friday")
+---> 15 print_message("Friday")
/Users/jhamrick/project/swc/novice/python/errors_02.py in print_message(day)
9 "sunday": "Aw, the weekend is almost over."
10 }
----> 11 print messages[day]
+---> 11 print messages[day]
12
13
diff --git a/07-errors.md b/07-errors.md
index c344de77d..798cc9074 100644
--- a/07-errors.md
+++ b/07-errors.md
@@ -38,14 +38,14 @@ favorite_ice_cream()
~~~ {.error}
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
-<ipython-input-1-9d0462a5b07c> in <module>()
+ in ()
1 from errors_01 import favorite_ice_cream
-----> 2 favorite_ice_cream()
+----> 2 favorite_ice_cream()
/Users/jhamrick/project/swc/novice/python/errors_01.pyc in favorite_ice_cream()
5 "strawberry"
6 ]
-----> 7 print ice_creams[3]
+----> 7 print ice_creams[3]
IndexError: list index out of range
~~~
@@ -125,7 +125,7 @@ def some_function()
return msg
~~~
~~~ {.error}
- File "<ipython-input-3-6bb841ea1423>", line 1
+ File "", line 1
def some_function()
^
SyntaxError: invalid syntax
@@ -147,7 +147,7 @@ def some_function():
return msg
~~~
~~~ {.error}
- File "<ipython-input-4-ae290e7659cb>", line 4
+ File "", line 4
return msg
^
IndentationError: unexpected indent
@@ -177,7 +177,7 @@ it *always* means that there is a problem with how your code is indented.
> return msg
> ~~~
> ~~~ {.error}
-> File "<ipython-input-5-653b36fbcd41>", line 4
+> File "", line 4
> return msg
> ^
> IndentationError: unindent does not match any outer indentation level
@@ -200,8 +200,8 @@ print a
~~~ {.error}
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
-<ipython-input-7-9d7b17ad5387> in <module>()
-----> 1 print a
+ in ()
+----> 1 print a
NameError: name 'a' is not defined
~~~
@@ -222,8 +222,8 @@ print hello
~~~ {.error}
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
-<ipython-input-8-9553ee03b645> in <module>()
-----> 1 print hello
+ in ()
+----> 1 print hello
NameError: name 'hello' is not defined
~~~
@@ -240,9 +240,9 @@ print "The count is: " + str(count)
~~~ {.error}
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
-<ipython-input-9-dd6a12d7ca5c> in <module>()
+ in ()
1 for number in range(10):
-----> 2 count = count + number
+----> 2 count = count + number
3 print "The count is: " + str(count)
NameError: name 'count' is not defined
@@ -263,10 +263,10 @@ print "The count is: " + str(count)
~~~ {.error}
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
-<ipython-input-10-d77d40059aea> in <module>()
+ in ()
1 Count = 0
2 for number in range(10):
-----> 3 count = count + number
+----> 3 count = count + number
4 print "The count is: " + str(count)
NameError: name 'count' is not defined
@@ -298,10 +298,10 @@ Letter #3 is c
~~~ {.error}
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
-<ipython-input-11-d817f55b7d6c> in <module>()
+ in ()
3 print "Letter #2 is " + letters[1]
4 print "Letter #3 is " + letters[2]
-----> 5 print "Letter #4 is " + letters[3]
+----> 5 print "Letter #4 is " + letters[3]
IndexError: list index out of range
~~~
@@ -323,10 +323,10 @@ print "The capital of Oregon is: " + us_state_capitals['oregon']
~~~ {.error}
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
-<ipython-input-12-27fa113dd73c> in <module>()
+ in ()
6 }
7
-----> 8 print "The capital of Oregon is: " + us_state_capitals['oregon']
+----> 8 print "The capital of Oregon is: " + us_state_capitals['oregon']
KeyError: 'oregon'
~~~
@@ -352,10 +352,10 @@ print "The capital of Massachusetts is: " + us_state_capitals['massachussetts']
~~~ {.error}
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
-<ipython-input-13-ae1dac4c6a45> in <module>()
+ in ()
6 }
7
-----> 8 print "The capital of Massachusetts is: " + us_state_capitals['massachussetts']
+----> 8 print "The capital of Massachusetts is: " + us_state_capitals['massachussetts']
KeyError: 'massachussetts'
~~~
@@ -377,8 +377,8 @@ file_handle = open('myfile.txt', 'r')
~~~ {.error}
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
-<ipython-input-14-f6e1ac4aee96> in <module>()
-----> 1 file_handle = open('myfile.txt', 'r')
+ in ()
+----> 1 file_handle = open('myfile.txt', 'r')
IOError: [Errno 2] No such file or directory: 'myfile.txt'
~~~
@@ -407,9 +407,9 @@ file_handle.read()
~~~ {.error}
---------------------------------------------------------------------------
IOError Traceback (most recent call last)
-<ipython-input-15-b846479bc61f> in <module>()
+ in ()
1 file_handle = open('myfile.txt', 'w')
-----> 2 file_handle.read()
+----> 2 file_handle.read()
IOError: File not open for reading
~~~
@@ -432,19 +432,19 @@ IOError: File not open for reading
> ~~~ {.error}
> ---------------------------------------------------------------------------
> KeyError Traceback (most recent call last)
-> <ipython-input-2-e4c4cbafeeb5> in <module>()
+> in ()
> 1 from errors_02 import print_friday_message
-> ----> 2 print_friday_message()
+> ----> 2 print_friday_message()
>
> /Users/jhamrick/project/swc/novice/python/errors_02.py in print_friday_message()
> 13
> 14 def print_friday_message():
-> ---> 15 print_message("Friday")
+> ---> 15 print_message("Friday")
>
> /Users/jhamrick/project/swc/novice/python/errors_02.py in print_message(day)
> 9 "sunday": "Aw, the weekend is almost over."
> 10 }
-> ---> 11 print messages[day]
+> ---> 11 print messages[day]
> 12
> 13
>