Skip to content

Commit

Permalink
Merge pull request #1005 from patrick-austin/remove-dictionaries-from…
Browse files Browse the repository at this point in the history
…-errors

Remove dictionary references from 09-errors.md
  • Loading branch information
noatgnu authored Mar 21, 2023
2 parents 5a5feae + 494ec09 commit e6c1c87
Showing 1 changed file with 69 additions and 68 deletions.
137 changes: 69 additions & 68 deletions _episodes/09-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,75 @@ as it is possible to create custom errors.
In that case,
hopefully the custom error message is informative enough to help you figure out what went wrong.

> ## Reading Error Messages
>
> Read the Python code and the resulting traceback below, and answer the following questions:
>
> 1. How many levels does the traceback have?
> 2. What is the function name where the error occurred?
> 3. On which line number in this function did the error occur?
> 4. What is the type of error?
> 5. What is the error message?
>
> ~~~
> # This code has an intentional error. Do not type it directly;
> # use it for reference to understand the error message below.
> def print_message(day):
> messages = [
> 'Hello, world!',
> 'Today is Tuesday!',
> 'It is the middle of the week.',
> 'Today is Donnerstag in German!',
> 'Last day of the week!',
> 'Hooray for the weekend!',
> 'Aw, the weekend is almost over.'
> ]
> print(messages[day])
>
> def print_sunday_message():
> print_message(7)
>
> print_sunday_message()
> ~~~
> {: .language-python}
>
> ~~~
> ---------------------------------------------------------------------------
> IndexError Traceback (most recent call last)
> <ipython-input-7-3ad455d81842> in <module>
> 16 print_message(7)
> 17
> ---> 18 print_sunday_message()
> 19
>
> <ipython-input-7-3ad455d81842> in print_sunday_message()
> 14
> 15 def print_sunday_message():
> ---> 16 print_message(7)
> 17
> 18 print_sunday_message()
>
> <ipython-input-7-3ad455d81842> in print_message(day)
> 11 'Aw, the weekend is almost over.'
> 12 ]
> ---> 13 print(messages[day])
> 14
> 15 def print_sunday_message():
>
> IndexError: list index out of range
> ~~~
> {: .error}
>
> > ## Solution
> > 1. 3 levels
> > 2. `print_message`
> > 3. 13
> > 4. `IndexError`
> > 5. `list index out of range` You can then infer that
> > `7` is not the right index to use with `messages`.
> {: .solution}
{: .challenge}
=======
> ## Better errors on newer Pythons
>
> Newer versions of Python have improved error printouts. If you are debugging errors, it is often
Expand Down Expand Up @@ -440,74 +509,6 @@ If you get an error that you've never seen before,
searching the Internet for that error type
often reveals common reasons why you might get that error.

> ## Reading Error Messages
>
> Read the Python code and the resulting traceback below, and answer the following questions:
>
> 1. How many levels does the traceback have?
> 2. What is the function name where the error occurred?
> 3. On which line number in this function did the error occur?
> 4. What is the type of error?
> 5. What is the error message?
>
> ~~~
> # This code has an intentional error. Do not type it directly;
> # use it for reference to understand the error message below.
> def print_message(day):
> messages = {
> 'monday': 'Hello, world!',
> 'tuesday': 'Today is Tuesday!',
> 'wednesday': 'It is the middle of the week.',
> 'thursday': 'Today is Donnerstag in German!',
> 'friday': 'Last day of the week!',
> 'saturday': 'Hooray for the weekend!',
> 'sunday': 'Aw, the weekend is almost over.'
> }
> print(messages[day])
>
> def print_friday_message():
> print_message('Friday')
>
> print_friday_message()
> ~~~
> {: .language-python}
>
> ~~~
> ---------------------------------------------------------------------------
> KeyError Traceback (most recent call last)
> <ipython-input-1-4be1945adbe2> in <module>()
> 14 print_message('Friday')
> 15
> ---> 16 print_friday_message()
>
> <ipython-input-1-4be1945adbe2> in print_friday_message()
> 12
> 13 def print_friday_message():
> ---> 14 print_message('Friday')
> 15
> 16 print_friday_message()
>
> <ipython-input-1-4be1945adbe2> in print_message(day)
> 9 'sunday': 'Aw, the weekend is almost over.'
> 10 }
> ---> 11 print(messages[day])
> 12
> 13 def print_friday_message():
>
> KeyError: 'Friday'
> ~~~
> {: .error}
>
> > ## Solution
> > 1. 3 levels
> > 2. `print_message`
> > 3. 11
> > 4. `KeyError`
> > 5. There isn't really a message; you're supposed
> > to infer that `Friday` is not a key in `messages`.
> {: .solution}
{: .challenge}

> ## Identifying Syntax Errors
>
> 1. Read the code below, and (without running it) try to identify what the errors are.
Expand Down

0 comments on commit e6c1c87

Please sign in to comment.