Skip to content

Commit e6c1c87

Browse files
authored
Merge pull request #1005 from patrick-austin/remove-dictionaries-from-errors
Remove dictionary references from 09-errors.md
2 parents 5a5feae + 494ec09 commit e6c1c87

File tree

1 file changed

+69
-68
lines changed

1 file changed

+69
-68
lines changed

_episodes/09-errors.md

Lines changed: 69 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,75 @@ as it is possible to create custom errors.
127127
In that case,
128128
hopefully the custom error message is informative enough to help you figure out what went wrong.
129129

130+
> ## Reading Error Messages
131+
>
132+
> Read the Python code and the resulting traceback below, and answer the following questions:
133+
>
134+
> 1. How many levels does the traceback have?
135+
> 2. What is the function name where the error occurred?
136+
> 3. On which line number in this function did the error occur?
137+
> 4. What is the type of error?
138+
> 5. What is the error message?
139+
>
140+
> ~~~
141+
> # This code has an intentional error. Do not type it directly;
142+
> # use it for reference to understand the error message below.
143+
> def print_message(day):
144+
> messages = [
145+
> 'Hello, world!',
146+
> 'Today is Tuesday!',
147+
> 'It is the middle of the week.',
148+
> 'Today is Donnerstag in German!',
149+
> 'Last day of the week!',
150+
> 'Hooray for the weekend!',
151+
> 'Aw, the weekend is almost over.'
152+
> ]
153+
> print(messages[day])
154+
>
155+
> def print_sunday_message():
156+
> print_message(7)
157+
>
158+
> print_sunday_message()
159+
> ~~~
160+
> {: .language-python}
161+
>
162+
> ~~~
163+
> ---------------------------------------------------------------------------
164+
> IndexError Traceback (most recent call last)
165+
> <ipython-input-7-3ad455d81842> in <module>
166+
> 16 print_message(7)
167+
> 17
168+
> ---> 18 print_sunday_message()
169+
> 19
170+
>
171+
> <ipython-input-7-3ad455d81842> in print_sunday_message()
172+
> 14
173+
> 15 def print_sunday_message():
174+
> ---> 16 print_message(7)
175+
> 17
176+
> 18 print_sunday_message()
177+
>
178+
> <ipython-input-7-3ad455d81842> in print_message(day)
179+
> 11 'Aw, the weekend is almost over.'
180+
> 12 ]
181+
> ---> 13 print(messages[day])
182+
> 14
183+
> 15 def print_sunday_message():
184+
>
185+
> IndexError: list index out of range
186+
> ~~~
187+
> {: .error}
188+
>
189+
> > ## Solution
190+
> > 1. 3 levels
191+
> > 2. `print_message`
192+
> > 3. 13
193+
> > 4. `IndexError`
194+
> > 5. `list index out of range` You can then infer that
195+
> > `7` is not the right index to use with `messages`.
196+
> {: .solution}
197+
{: .challenge}
198+
=======
130199
> ## Better errors on newer Pythons
131200
>
132201
> Newer versions of Python have improved error printouts. If you are debugging errors, it is often
@@ -440,74 +509,6 @@ If you get an error that you've never seen before,
440509
searching the Internet for that error type
441510
often reveals common reasons why you might get that error.
442511
443-
> ## Reading Error Messages
444-
>
445-
> Read the Python code and the resulting traceback below, and answer the following questions:
446-
>
447-
> 1. How many levels does the traceback have?
448-
> 2. What is the function name where the error occurred?
449-
> 3. On which line number in this function did the error occur?
450-
> 4. What is the type of error?
451-
> 5. What is the error message?
452-
>
453-
> ~~~
454-
> # This code has an intentional error. Do not type it directly;
455-
> # use it for reference to understand the error message below.
456-
> def print_message(day):
457-
> messages = {
458-
> 'monday': 'Hello, world!',
459-
> 'tuesday': 'Today is Tuesday!',
460-
> 'wednesday': 'It is the middle of the week.',
461-
> 'thursday': 'Today is Donnerstag in German!',
462-
> 'friday': 'Last day of the week!',
463-
> 'saturday': 'Hooray for the weekend!',
464-
> 'sunday': 'Aw, the weekend is almost over.'
465-
> }
466-
> print(messages[day])
467-
>
468-
> def print_friday_message():
469-
> print_message('Friday')
470-
>
471-
> print_friday_message()
472-
> ~~~
473-
> {: .language-python}
474-
>
475-
> ~~~
476-
> ---------------------------------------------------------------------------
477-
> KeyError Traceback (most recent call last)
478-
> <ipython-input-1-4be1945adbe2> in <module>()
479-
> 14 print_message('Friday')
480-
> 15
481-
> ---> 16 print_friday_message()
482-
>
483-
> <ipython-input-1-4be1945adbe2> in print_friday_message()
484-
> 12
485-
> 13 def print_friday_message():
486-
> ---> 14 print_message('Friday')
487-
> 15
488-
> 16 print_friday_message()
489-
>
490-
> <ipython-input-1-4be1945adbe2> in print_message(day)
491-
> 9 'sunday': 'Aw, the weekend is almost over.'
492-
> 10 }
493-
> ---> 11 print(messages[day])
494-
> 12
495-
> 13 def print_friday_message():
496-
>
497-
> KeyError: 'Friday'
498-
> ~~~
499-
> {: .error}
500-
>
501-
> > ## Solution
502-
> > 1. 3 levels
503-
> > 2. `print_message`
504-
> > 3. 11
505-
> > 4. `KeyError`
506-
> > 5. There isn't really a message; you're supposed
507-
> > to infer that `Friday` is not a key in `messages`.
508-
> {: .solution}
509-
{: .challenge}
510-
511512
> ## Identifying Syntax Errors
512513
>
513514
> 1. Read the code below, and (without running it) try to identify what the errors are.

0 commit comments

Comments
 (0)