Skip to content

Make errors example into a function #21

Closed

Description

@SJaffa

https://github.com/UoMResearchIT/programming_with_python/blob/19edfb758c78c9614ae7a2406dfa581a473f47ac/_episodes/05-defensive_programming.md?plain=1#L29

We could make this into a function so it is easier to add to it rather than copy-pasting, and clearer that the person giving the input value might be a different person.

Activity

  1. Scottan commented on Nov 14, 2025

    @Scottan
    Collaborator

    I thought the same when giving the course on Tuesday, would have let me focus on the important parts of the lesson more.

    I also find the wording of the test a bit weird. I would recommend:

    > def test_number(val):
    >     if val == 0:
    >         print('Value ', val, 'is zero')
    >     elif val > -10 and val < 10:
    >         print('Value: ', val, 'is a single digit.')
    >     else:
    >         print('Value: ', val, 'is a multi-digit number.')
    

    Honestly, the test itself is fairly unimportant, although I think making it clear that negative values are ok is a good idea because we had a student ask if this was the issue. Checking for negative, 0 or positive would also be fine, and probably simpler so we keep it focussed on what's important for the lesson:

    > def check_sign(val):
    >     if val == 0:
    >         print('Value ', val, 'is zero')
    >     elif val > 0:
    >         print('Value: ', val, 'is positive')
    >     else:
    >         print('Value: ', val, 'is negative')
    
  2. Scottan commented on Nov 14, 2025

    @Scottan
    Collaborator

    p.s. adding suggestion from @AnnieZQH to add a box in this section on use of try / except referring to:

    https://docs.python.org/3.6/glossary.html#term-eafp

  3. linked a pull request that will close this issue21 make errors example into a function #56on Nov 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions