4.1. Exercises
Exceptions
Documenting Exceptions
def factor_of_10(number):
"""
Takes a number and determines if it is a factor of 10
Parameters
----------
number : int
the value to check
Returns
-------
bool
Returns True if number is a multiple of 10 and False otherwise
Raises
------
TypeError
If the input argument number is not of type int
Examples
--------
>>> factor_of_10(72)
False
"""
if not isinstance(number, int):
raise TypeError("the input value of number is not of type int")
if number % 10 == 0:
factor = True
else:
factor = False
return factordef factor_of_10(number):
"""
Takes a number and determines if it is a factor of 10
Parameters
----------
number : int
the value to check
Returns
-------
bool
Returns True if number is a multiple of 10 and False otherwise
Exceptions
------
TypeError
If the input argument number is not of type int
Examples
--------
>>> factor_of_10(72)
False
"""
if not isinstance(number, int):
raise TypeError("the input value of number is not of type int")
if number % 10 == 0:
factor = True
else:
factor = False
return factorRaising Exceptions
Instructions:
Running a coding exercise for the first time could take a bit of time for everything to load. Be patient, it could take a few minutes.
When you see ____ in a coding exercise, replace it with what you assume to be the correct code. Run it and see if you obtain the desired output. Submit your code to validate if you were correct.
Make sure you remove the hash (#) symbol in the coding portions of this question. We have commented them so that the line won’t execute and you can test your code after each step.
Let’s build on the BMI function we made in module 5. This time we want to raise 2 exceptions.
def bmi_calculator(height, weight):
return (weight / (height ** 2)) * 702Tasks:
- Write an exception that checks if
heightis of typefloat. - Write a second exception that raises an error if weight is 0 or less.
- Test your function with the values given in variable
tallandmass.