{"record":{"id":"b95b557da0b633de","repo":"TheAlgorithms/Python","slug":"cash-flows-list-cannot-be-empty","errorCode":null,"errorMessage":"Cash flows list cannot be empty","messagePattern":"Cash flows list cannot be empty","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"financial/present_value.py","lineNumber":32,"sourceCode":"    >>> present_value(0.13, [10, 20.70, -293, 297])\n    4.69\n    >>> present_value(0.07, [-109129.39, 30923.23, 15098.93, 29734,39])\n    -42739.63\n    >>> present_value(0.07, [109129.39, 30923.23, 15098.93, 29734,39])\n    175519.15\n    >>> present_value(-1, [109129.39, 30923.23, 15098.93, 29734,39])\n    Traceback (most recent call last):\n        ...\n    ValueError: Discount rate cannot be negative\n    >>> present_value(0.03, [])\n    Traceback (most recent call last):\n        ...\n    ValueError: Cash flows list cannot be empty\n    \"\"\"\n    if discount_rate < 0:\n        raise ValueError(\"Discount rate cannot be negative\")\n    if not cash_flows:\n        raise ValueError(\"Cash flows list cannot be empty\")\n    present_value = sum(\n        cash_flow / ((1 + discount_rate) ** i) for i, cash_flow in enumerate(cash_flows)\n    )\n    return round(present_value, ndigits=2)\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":14,"sourceCodeEnd":43,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/financial/present_value.py#L14-L43","documentation":"Raised by present_value() when cash_flows is empty (falsy). Summing discounts over zero flows is defined mathematically as 0, but the library treats an empty schedule as caller error — most likely a missing or misparsed input — and rejects it before the sum() generator runs.","triggerScenarios":"Calling present_value(0.03, []) or with any falsy flows value (empty tuple, None). Reached only after the discount-rate check passes.","commonSituations":"Parsing a CSV/date-range filter that yields no rows, a typo'd schedule variable defaulting to [], or passing None for the flows argument.","solutions":["Check the flows list is non-empty before calling; investigate why it is empty.","Fix the upstream filter/parsing that produced zero cash flows.","If an empty schedule is legitimate in your domain, return 0 explicitly in your wrapper rather than calling the function."],"exampleFix":"# before\nflows = [f for f in all_flows if f.date <= cutoff]  # cutoff too early -> []\npv = present_value(0.03, flows)\n\n# after\nflows = [f for f in all_flows if f.date <= cutoff]\nif not flows:\n    raise ValueError('no cash flows in window')\npv = present_value(0.03, flows)","handlingStrategy":"validation","validationCode":"if not cash_flows:\n    raise ValueError('cash flow schedule is empty')\npresent_value(rate, cash_flows)","typeGuard":"def is_nonempty_numeric_flows(flows: object) -> bool:\n    return isinstance(flows, list) and len(flows) > 0 and all(\n        isinstance(f, (int, float)) for f in flows\n    )","tryCatchPattern":"try:\n    pv = present_value(r, flows)\nexcept ValueError as exc:\n    if 'Cash flows' in str(exc):\n        pv = 0.0\n    else:\n        raise","preventionTips":["Assert non-empty inputs after date/window filters.","Beware falsy values: present_value(0.03, None) hits this same branch, not a TypeError."],"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"}