This project contains two main files: Grades.py, which implements functions to calculate the total, average, and median of a list of numbers, and test_grades.py, which contains unit tests to ensure the correctness of those functions. The program handles typical scenarios such as calculating values for lists with numbers, handling empty lists gracefully, and providing precise floating-point results where necessary.
-
total(values: list) -> float
Returns the sum of all elements in thevalueslist. If the list is empty, it returns 0. -
average(values: list) -> float
Returns the arithmetic mean (average) of the elements in thevalueslist. If the list is empty, it returnsmath.nan. -
median(values: list) -> float
Returns the median of the elements in thevalueslist. If the list is empty, it raises aValueError. For an even number of elements, it returns the average of the two middle values.
The test_grades.py module contains unit tests to verify the functionality of the Grades.py functions:
-
Total Function Tests:
- Verifies that the
totalfunction correctly sums a list of numbers. - Ensures the function returns 0 when given an empty list.
- Verifies that the
-
Average Function Tests:
- Verifies the average function calculates the correct mean of a list.
- Ensures precision by checking up to 5 decimal places.
- Checks that the function returns
math.nanwhen given an empty list.
-
Median Function Tests:
- Checks that the median function works for both odd and even-length lists.
- Verifies that a
ValueErroris raised when given an empty list.
- Clone or download this repository to your local machine.
- Ensure Python 3.x is installed on your machine.
- To run the unit tests, make sure
unittestis available (it comes with Python by default). - Run the tests and you should get an output with the tests ran and the time it took to run them.