diff --git a/maths/area.py b/maths/area.py index 3a0fd9739..0cbfd7957 100644 --- a/maths/area.py +++ b/maths/area.py @@ -12,7 +12,13 @@ def surface_area_cube(side_length: float) -> float: 6 >>> surface_area_cube(3) 54 + >>> surface_area_cube(-1) + Traceback (most recent call last): + ... + ValueError: surface_area_cube() only accepts non-negative values """ + if side_length < 0: + raise ValueError("surface_area_cube() only accepts non-negative values") return 6 * side_length ** 2 @@ -26,7 +32,13 @@ def surface_area_sphere(radius: float) -> float: 314.1592653589793 >>> surface_area_sphere(1) 12.566370614359172 + >>> surface_area_sphere(-1) + Traceback (most recent call last): + ... + ValueError: surface_area_sphere() only accepts non-negative values """ + if radius < 0: + raise ValueError("surface_area_sphere() only accepts non-negative values") return 4 * pi * radius ** 2 @@ -34,9 +46,23 @@ def area_rectangle(length: float, width: float) -> float: """ Calculate the area of a rectangle - >>> area_rectangle(10,20) + >>> area_rectangle(10, 20) 200 + >>> area_rectangle(-1, -2) + Traceback (most recent call last): + ... + ValueError: area_rectangle() only accepts non-negative values + >>> area_rectangle(1, -2) + Traceback (most recent call last): + ... + ValueError: area_rectangle() only accepts non-negative values + >>> area_rectangle(-1, 2) + Traceback (most recent call last): + ... + ValueError: area_rectangle() only accepts non-negative values """ + if length < 0 or width < 0: + raise ValueError("area_rectangle() only accepts non-negative values") return length * width @@ -46,7 +72,13 @@ def area_square(side_length: float) -> float: >>> area_square(10) 100 + >>> area_square(-1) + Traceback (most recent call last): + ... + ValueError: area_square() only accepts non-negative values """ + if side_length < 0: + raise ValueError("area_square() only accepts non-negative values") return side_length ** 2 @@ -54,9 +86,23 @@ def area_triangle(base: float, height: float) -> float: """ Calculate the area of a triangle - >>> area_triangle(10,10) + >>> area_triangle(10, 10) 50.0 + >>> area_triangle(-1, -2) + Traceback (most recent call last): + ... + ValueError: area_triangle() only accepts non-negative values + >>> area_triangle(1, -2) + Traceback (most recent call last): + ... + ValueError: area_triangle() only accepts non-negative values + >>> area_triangle(-1, 2) + Traceback (most recent call last): + ... + ValueError: area_triangle() only accepts non-negative values """ + if base < 0 or height < 0: + raise ValueError("area_triangle() only accepts non-negative values") return (base * height) / 2 @@ -64,9 +110,23 @@ def area_parallelogram(base: float, height: float) -> float: """ Calculate the area of a parallelogram - >>> area_parallelogram(10,20) + >>> area_parallelogram(10, 20) 200 + >>> area_parallelogram(-1, -2) + Traceback (most recent call last): + ... + ValueError: area_parallelogram() only accepts non-negative values + >>> area_parallelogram(1, -2) + Traceback (most recent call last): + ... + ValueError: area_parallelogram() only accepts non-negative values + >>> area_parallelogram(-1, 2) + Traceback (most recent call last): + ... + ValueError: area_parallelogram() only accepts non-negative values """ + if base < 0 or height < 0: + raise ValueError("area_parallelogram() only accepts non-negative values") return base * height @@ -74,9 +134,39 @@ def area_trapezium(base1: float, base2: float, height: float) -> float: """ Calculate the area of a trapezium - >>> area_trapezium(10,20,30) + >>> area_trapezium(10, 20, 30) 450.0 + >>> area_trapezium(-1, -2, -3) + Traceback (most recent call last): + ... + ValueError: area_trapezium() only accepts non-negative values + >>> area_trapezium(-1, 2, 3) + Traceback (most recent call last): + ... + ValueError: area_trapezium() only accepts non-negative values + >>> area_trapezium(1, -2, 3) + Traceback (most recent call last): + ... + ValueError: area_trapezium() only accepts non-negative values + >>> area_trapezium(1, 2, -3) + Traceback (most recent call last): + ... + ValueError: area_trapezium() only accepts non-negative values + >>> area_trapezium(-1, -2, 3) + Traceback (most recent call last): + ... + ValueError: area_trapezium() only accepts non-negative values + >>> area_trapezium(1, -2, -3) + Traceback (most recent call last): + ... + ValueError: area_trapezium() only accepts non-negative values + >>> area_trapezium(-1, 2, -3) + Traceback (most recent call last): + ... + ValueError: area_trapezium() only accepts non-negative values """ + if base1 < 0 or base2 < 0 or height < 0: + raise ValueError("area_trapezium() only accepts non-negative values") return 1 / 2 * (base1 + base2) * height @@ -86,7 +176,13 @@ def area_circle(radius: float) -> float: >>> area_circle(20) 1256.6370614359173 + >>> area_circle(-1) + Traceback (most recent call last): + ... + ValueError: area_circle() only accepts non-negative values """ + if radius < 0: + raise ValueError("area_circle() only accepts non-negative values") return pi * radius ** 2