Your Turn: Coder-Friendly Hints

A try...except...else...finally statement can have more than one except.  Also, each except can have one or more statements that address specific errors.  For example, instead of displaying the exception information, you can display a message that explains how to solve the problem.  

  • Save a copy of exceptions_division_calc_try_this as exceptions_division_calc_your_turn.
  • Replace this block:
except Exception as e:
    print("Exception = ", e)
    et = type(e)
    print("Exception type = ", et)

...with this:

except ZeroDivisionError:
    print("Can't divide by zero.")

except TypeError:
        print("Expected a number.")

 

Now let's repeatt the tests three times, changing the value of d each time:

  • d = 5
  • d = 0
  • d = "Hello"

 

  • Run the script with d = 5.
  • Verify that the script provides a quotient result when when d is set to a non-zero integer, in this case, 5:

 

  • Then test dividing by zero with d = 0
  • Verify that the script displays "Can’t divide by zero." when d is set to 0.

 

  • Finally, test it one more time, using d = "Hello".
  • Verify that the script displays "Expected a number."