{"record":{"id":"b56e9dddb0dd1ed3","repo":"TheAlgorithms/Python","slug":"useful-years-cannot-be-less-than-1","errorCode":null,"errorMessage":"Useful years cannot be less than 1","messagePattern":"Useful years cannot be less than 1","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"financial/straight_line_depreciation.py","lineNumber":59,"sourceCode":"    :param residual_value: Residual value of the asset at the end of its useful life\n    :return: A list of annual depreciation expenses over the asset's useful life\n    >>> straight_line_depreciation(10, 1100.0, 100.0)\n    [100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0, 100.0]\n    >>> straight_line_depreciation(6, 1250.0, 50.0)\n    [200.0, 200.0, 200.0, 200.0, 200.0, 200.0]\n    >>> straight_line_depreciation(4, 1001.0)\n    [250.25, 250.25, 250.25, 250.25]\n    >>> straight_line_depreciation(11, 380.0, 50.0)\n    [30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0, 30.0]\n    >>> straight_line_depreciation(1, 4985, 100)\n    [4885.0]\n    \"\"\"\n\n    if not isinstance(useful_years, int):\n        raise TypeError(\"Useful years must be an integer\")\n\n    if useful_years < 1:\n        raise ValueError(\"Useful years cannot be less than 1\")\n\n    if not isinstance(purchase_value, (float, int)):\n        raise TypeError(\"Purchase value must be numeric\")\n\n    if not isinstance(residual_value, (float, int)):\n        raise TypeError(\"Residual value must be numeric\")\n\n    if purchase_value < 0.0:\n        raise ValueError(\"Purchase value cannot be less than zero\")\n\n    if purchase_value < residual_value:\n        raise ValueError(\"Purchase value cannot be less than residual value\")\n\n    # Calculate annual depreciation expense\n    depreciable_cost = purchase_value - residual_value\n    annual_depreciation_expense = depreciable_cost / useful_years\n\n    # List of annual depreciation expenses","sourceCodeStart":41,"sourceCodeEnd":77,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/financial/straight_line_depreciation.py#L41-L77","documentation":"Raised by straight_line_depreciation() when useful_years is an int but < 1. range(useful_years) would produce no periods, so a zero or negative asset life is rejected. This check runs after the isinstance check, so the value is guaranteed integral when it fires.","triggerScenarios":"Calling straight_line_depreciation(0, 1250.0, 50.0) or with a negative int such as -3.","commonSituations":"Lifespans computed as (retire_year - acquire_year) that hit 0 for same-year assets, config placeholders of 0 meaning 'not set', or sign errors from data entry.","solutions":["Pass useful_years >= 1; the minimum meaningful schedule is one full-year period.","Guard computed lifespans and treat 0 as missing data rather than clamping silently.","Reject zero-life assets upstream — they usually indicate missing acquisition data."],"exampleFix":"# before\nlife = retire_year - acquire_year  # 0 for same-year assets\nstraight_line_depreciation(life, 1250.0, 50.0)\n\n# after\nlife = retire_year - acquire_year\nif life < 1:\n    raise ValueError(f'asset life must be >= 1 year, got {life}')\nstraight_line_depreciation(life, 1250.0, 50.0)","handlingStrategy":"validation","validationCode":"if not isinstance(useful_years, int) or useful_years < 1:\n    raise ValueError('useful_years must be an int >= 1')\nstraight_line_depreciation(useful_years, purchase, residual)","typeGuard":null,"tryCatchPattern":"try:\n    sched = straight_line_depreciation(y, p, r)\nexcept ValueError as exc:\n    if 'less than 1' in str(exc):\n        raise ValueError(f'invalid asset life: {y}') from exc\n    raise","preventionTips":["Treat a 0-year life as a data-quality alarm, not a clamp-to-1 case.","This raises ValueError while type problems raise TypeError — order your except clauses accordingly."],"tags":["finance","input-validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}