Here are some exercises for you to try on your own.
Import module math, and use its functions to complete the following exercises. You can call dir(math) to get a listing of the items in math.
In the following exercises, you will work with Python’s calendar module:
Create a file named exercise.py with this code inside it:
| | def average(num1: float, num2: float) -> float: |
| | """Return the average of num1 and num2. |
| | |
| | >>> average(10,20) |
| | 15.0 |
| | >>> average(2.5, 3.0) |
| | 2.75 |
| | """ |
| | |
| | return num1 + num2 / 2 |
Run exercise.py. Import doctest and run doctest.testmod.
Both of the tests in the function average’s docstring fail. Fix the code and rerun the tests. Repeat this procedure until the tests pass.
Footnotes