{"record":{"id":"4f6c31ab20d0870b","repo":"TheAlgorithms/Python","slug":"purchase-value-must-be-numeric","errorCode":null,"errorMessage":"Purchase value must be numeric","messagePattern":"Purchase value must be numeric","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"financial/straight_line_depreciation.py","lineNumber":62,"sourceCode":"    [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\n    list_of_depreciation_expenses = []\n    accumulated_depreciation_expense = 0.0\n    for period in range(useful_years):","sourceCodeStart":44,"sourceCodeEnd":80,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/financial/straight_line_depreciation.py#L44-L80","documentation":"Raised by straight_line_depreciation() when purchase_value is not int or float (TypeError). The arithmetic (purchase - residual, division by years) requires a real number, so strings, None, and Decimal are rejected before any math. bool passes because bool subclasses int.","triggerScenarios":"Calling straight_line_depreciation(5, '1250.0', 50.0), passing None for an optional field, or passing a decimal.Decimal from a money library. Fires after the useful_years checks pass.","commonSituations":"Currency amounts arriving as strings from CSV/JSON/HTML scraping, Decimal from database numeric columns, or None defaults from sparse records.","solutions":["Convert to float before calling: float(purchase_value) — mind precision for money-critical use.","Better: keep money in int cents if precision matters; this API accepts plain numbers only.","Fix ingestion: parse numeric fields at the edge, never in the accounting core."],"exampleFix":"# before\nstraight_line_depreciation(5, row['purchase_value'], 50.0)  # row value is '1250.0'\n\n# after\nstraight_line_depreciation(5, float(row['purchase_value']), 50.0)","handlingStrategy":"type-guard","validationCode":"if not isinstance(purchase_value, (int, float)) or isinstance(purchase_value, bool):\n    purchase_value = float(purchase_value)  # or raise\nstraight_line_depreciation(5, purchase_value, 50.0)","typeGuard":"def is_numeric(v: object) -> bool:\n    return isinstance(v, (int, float)) and not isinstance(v, bool)","tryCatchPattern":"try:\n    sched = straight_line_depreciation(y, p, r)\nexcept TypeError as exc:\n    if 'Purchase value must be numeric' in str(exc):\n        sched = straight_line_depreciation(y, float(p), r)\n    else:\n        raise","preventionTips":["Decimal does not pass isinstance((int, float)) — convert explicitly.","Parse currency strings once at the ingestion layer."],"tags":["finance","type-checking","typeerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}