{"record":{"id":"bacef417da89da50","repo":"TheAlgorithms/Python","slug":"purchase-value-cannot-be-less-than-residual-value","errorCode":null,"errorMessage":"Purchase value cannot be less than residual value","messagePattern":"Purchase value cannot be less than residual value","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"financial/straight_line_depreciation.py","lineNumber":71,"sourceCode":"    \"\"\"\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\n    list_of_depreciation_expenses = []\n    accumulated_depreciation_expense = 0.0\n    for period in range(useful_years):\n        if period != useful_years - 1:\n            accumulated_depreciation_expense += annual_depreciation_expense\n            list_of_depreciation_expenses.append(annual_depreciation_expense)\n        else:\n            depreciation_expense_in_end_year = (\n                depreciable_cost - accumulated_depreciation_expense\n            )\n            list_of_depreciation_expenses.append(depreciation_expense_in_end_year)\n","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/financial/straight_line_depreciation.py#L53-L89","documentation":"Raised by straight_line_depreciation() when purchase_value < residual_value (ValueError). The depreciable cost (purchase - residual) would be negative, producing negative annual depreciation — economically nonsensical for straight-line accounting, so the function refuses. Equal values are fine and yield all-zero schedules.","triggerScenarios":"Calling straight_line_depreciation(5, 50.0, 1250.0) (arguments swapped — the most common cause), or genuinely passing a salvage estimate above the recorded cost basis.","commonSituations":"Swapped positional arguments (purchase/residual order confusion), re-valuation where market salvage exceeds depreciated book cost, or unit mismatches (purchase in cents, residual in dollars).","solutions":["Check argument order: the signature is (useful_years, purchase_value, residual_value).","If order is right, fix the data: a residual above cost usually means a stale estimate or mismatched units.","Use keyword arguments at call sites to make ordering impossible to get wrong."],"exampleFix":"# before\nstraight_line_depreciation(5, residual, purchase)  # swapped\n\n# after\nstraight_line_depreciation(5, purchase, residual)  # or use keywords","handlingStrategy":"validation","validationCode":"if purchase_value < residual_value:\n    raise ValueError(\n        f'cost basis {purchase_value} below salvage {residual_value}'\n    )\nstraight_line_depreciation(5, purchase_value, residual_value)","typeGuard":null,"tryCatchPattern":"try:\n    sched = straight_line_depreciation(y, p, r)\nexcept ValueError as exc:\n    if 'less than residual' in str(exc):\n        raise ValueError('check purchase/residual order or units') from exc\n    raise","preventionTips":["Call with positional order (years, purchase, residual) or keywords to eliminate swap bugs.","Compare units (cents vs dollars) between the two money arguments before calling."],"tags":["finance","input-validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}