{"record":{"id":"fc8c0bb146ae5729","repo":"TheAlgorithms/Python","slug":"area-square-only-accepts-non-negative-values","errorCode":null,"errorMessage":"area_square() only accepts non-negative values","messagePattern":"area_square\\(\\) only accepts non-negative values","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/area.py","lineNumber":283,"sourceCode":"\r\n\r\ndef area_square(side_length: float) -> float:\r\n    \"\"\"\r\n    Calculate the area of a square.\r\n\r\n    >>> area_square(10)\r\n    100\r\n    >>> area_square(0)\r\n    0\r\n    >>> area_square(1.6)\r\n    2.5600000000000005\r\n    >>> area_square(-1)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_square() only accepts non-negative values\r\n    \"\"\"\r\n    if side_length < 0:\r\n        raise ValueError(\"area_square() only accepts non-negative values\")\r\n    return side_length**2\r\n\r\n\r\ndef area_triangle(base: float, height: float) -> float:\r\n    \"\"\"\r\n    Calculate the area of a triangle given the base and height.\r\n\r\n    >>> area_triangle(10, 10)\r\n    50.0\r\n    >>> area_triangle(1.6, 2.6)\r\n    2.08\r\n    >>> area_triangle(0, 0)\r\n    0.0\r\n    >>> area_triangle(-1, -2)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: area_triangle() only accepts non-negative values\r\n    >>> area_triangle(1, -2)\r","sourceCodeStart":265,"sourceCodeEnd":301,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/area.py#L265-L301","documentation":"area_square() raises this ValueError when side_length is negative. A square's side is a physical length; the library checks side_length >= 0 before returning side_length**2. The guard prevents a negative input from being silently squared into a plausible-looking but invalid area.","triggerScenarios":"Calling area_square(side_length) with side_length < 0, e.g. area_square(-1).","commonSituations":"Passing deltas or differences as sides; user input not validated for sign; data feeds that encode missing values as negative numbers.","solutions":["Check side_length >= 0 before the call and fix the negative source.","Use abs() if the sign is an artifact of how the length was computed.","Validate form/CLI input rejects negatives at entry.","Catch ValueError to present a clear error."],"exampleFix":"// before\narea = area_square(side)  # side may be negative from user input\n\n# after\nside = float(input('side: '))\nif side < 0:\n    raise ValueError('side must be non-negative')\narea = area_square(side)","handlingStrategy":"validation","validationCode":"if side_length < 0:\n    raise ValueError(f'square side must be >= 0, got {side_length}')\narea = area_square(side_length)","typeGuard":"def is_valid_side(x: object) -> bool:\n    return isinstance(x, (int, float)) and not isinstance(x, bool) and x >= 0","tryCatchPattern":"try:\n    area = area_square(side_length)\nexcept ValueError as e:\n    raise ValueError(f'invalid square input {side_length!r}: {e}') from e","preventionTips":["Reject negative numbers in input forms before they reach math code.","Use abs() on coordinate-derived sides."],"tags":["python","math","geometry","validation","valueerror","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}