An Abundance of Caution¶
When we create functions for other developers to use, we should ensure they are passing the correct arguments to our functions. This will help guide them when they attempt to use our code.
Create a function that takes a percent values from
0.0
to1.0
and converts it to a string from"0%"
to"100%"
. Use theround()
function to make it appear with no decimal places.Create a docstring for this function and write type annotations.
Create a
main
function that will make use of the percent conversion function.Raise an exception in your function when it recieves a value outside its defined range (
0.0
to1.0
). This is a reasonable check to perform.
If the programer using your function decides to ignore your docstring and annotation by passing a str
insead of a float
, that’s really on them. Whatever side effect that comes from that is their responsibility. You could equip your function to check that issue as well…
Ensure your function recieves a
float
number. Raise an exception if not. Use Python’stype()
function. Note: this is a different type of error than question #4.
©2021 Daniel Gallo
This work is licensed under Attribution-NonCommercial-ShareAlike 4.0 International