@@ -32,22 +32,34 @@ called a [traceback]({{ page.root }}/reference/#traceback).
3232Let's examine one:
3333
3434~~~
35- import errors_01
36- errors_01.favorite_ice_cream()
35+ # This code has an intentional error. You can type it directly or
36+ # use it for reference to understand the error message below.
37+ def favorite_ice_cream():
38+ ice_creams = [
39+ "chocolate",
40+ "vanilla",
41+ "strawberry"
42+ ]
43+ print(ice_creams[3])
44+
45+ favorite_ice_cream()
3746~~~
3847{: .python}
3948
4049~~~
4150---------------------------------------------------------------------------
4251IndexError Traceback (most recent call last)
43- <ipython-input-1-9d0462a5b07c> in <module>()
44- 1 import errors_01
45- ----> 2 errors_01.favorite_ice_cream()
46-
47- /Users/jhamrick/project/swc/novice/python/errors_01.pyc in favorite_ice_cream()
48- 5 "strawberry"
49- 6 ]
50- ----> 7 print(ice_creams[3])
52+ <ipython-input-1-70bd89baa4df> in <module>()
53+ 6 print(ice_creams[3])
54+ 7
55+ ----> 8 favorite_ice_cream()
56+
57+ <ipython-input-1-70bd89baa4df> in favorite_ice_cream()
58+ 4 "vanilla", "strawberry"
59+ 5 ]
60+ ----> 6 print(ice_creams[3])
61+ 7
62+ 8 favorite_ice_cream()
5163
5264IndexError: list index out of range
5365~~~
@@ -58,16 +70,16 @@ You can determine the number of levels by looking for the number of arrows on th
5870In this case:
5971
60721 . The first shows code from the cell above,
61- with an arrow pointing to Line 2 (which is ` favorite_ice_cream() ` ).
73+ with an arrow pointing to Line 8 (which is ` favorite_ice_cream() ` ).
6274
63- 2 . The second shows some code in another function ( ` favorite_ice_cream ` , located in the file ` errors_01.py ` ) ,
64- with an arrow pointing to Line 7 (which is ` print(ice_creams[3]) ` ).
75+ 2 . The second shows some code in the function ` favorite_ice_cream ` ,
76+ with an arrow pointing to Line 6 (which is ` print(ice_creams[3]) ` ).
6577
6678The last level is the actual place where the error occurred.
6779The other level(s) show what function the program executed to get to the next level down.
6880So, in this case, the program first performed a [ function call] ({{ page.root }}/reference/#function-call) to the function ` favorite_ice_cream ` .
6981Inside this function,
70- the program encountered an error on Line 7 , when it tried to run the code ` print(ice_creams[3]) ` .
82+ the program encountered an error on Line 6 , when it tried to run the code ` print(ice_creams[3]) ` .
7183
7284> ## Long Tracebacks
7385>
@@ -410,51 +422,68 @@ often reveals common reasons why you might get that error.
410422
411423> ## Reading Error Messages
412424>
413- > Read the traceback below, and identify the following pieces of information about it :
425+ > Read the python code and the resulting traceback below, and answer the following questions :
414426>
415427> 1. How many levels does the traceback have?
416- > 2. What is the file name where the error occurred?
417- > 3. What is the function name where the error occurred?
418- > 4. On which line number in this function did the error occurr?
419- > 5. What is the type of error?
420- > 6. What is the error message?
428+ > 2. What is the function name where the error occurred?
429+ > 3. On which line number in this function did the error occurr?
430+ > 4. What is the type of error?
431+ > 5. What is the error message?
421432>
422433> ~~~
423- > import errors_02
424- > errors_02.print_friday_message()
434+ > # This code has an intentional error. Do not type it directly;
435+ > # use it for reference to understand the error message below.
436+ > def print_message(day):
437+ > messages = {
438+ > "monday": "Hello, world!",
439+ > "tuesday": "Today is tuesday!",
440+ > "wednesday": "It is the middle of the week.",
441+ > "thursday": "Today is Donnerstag in German!",
442+ > "friday": "Last day of the week!",
443+ > "saturday": "Hooray for the weekend!",
444+ > "sunday": "Aw, the weekend is almost over."
445+ > }
446+ > print(messages[day])
447+ >
448+ > def print_friday_message():
449+ > print_message("Friday")
450+ >
451+ > print_friday_message()
425452> ~~~
426453> {: .python}
427454>
428455> ~~~
429456> ---------------------------------------------------------------------------
430457> KeyError Traceback (most recent call last)
431- > <ipython-input-2-e4c4cbafeeb5> in <module>()
432- > 1 import errors_02
433- > ----> 2 errors_02.print_friday_message()
458+ > <ipython-input-1-4be1945adbe2> in <module>()
459+ > 14 print_message("Friday")
460+ > 15
461+ > ---> 16 print_friday_message()
434462>
435- > /Users/jhamrick/project/swc/novice/python/errors_02.py in print_friday_message()
436- > 13
437- > 14 def print_friday_message():
438- > ---> 15 print_message("Friday")
463+ > <ipython-input-1-4be1945adbe2> in print_friday_message()
464+ > 12
465+ > 13 def print_friday_message():
466+ > ---> 14 print_message("Friday")
467+ > 15
468+ > 16 print_friday_message()
439469>
440- > /Users/jhamrick/project/swc/novice/python/errors_02.py in print_message(day)
470+ > <ipython-input-1-4be1945adbe2> in print_message(day)
441471> 9 "sunday": "Aw, the weekend is almost over."
442472> 10 }
443473> ---> 11 print(messages[day])
444474> 12
445- > 13
475+ > 13 def print_friday_message():
446476>
447477> KeyError: 'Friday'
448478> ~~~
449479> {: .error}
450480>
451481> > ## Solution
452482> > 1. 3 levels
453- > > 2. `errors_02.py`
454- > > 3. `print_message`
455- > > 4. 11
456- > > 5. `KeyError`
457- > > 6. There isn't really a message; you're supposed to infer that `Friday` is not a key in `messages`.
483+ > > 2. `print_message`
484+ > > 3. 11
485+ > > 4. `KeyError`
486+ > > 5. There isn't really a message; you're supposed to infer that `Friday` is not a key in `messages`.
458487> {: .solution}
459488{: .challenge}
460489
0 commit comments