{"record":{"id":"8805d1bc359605a4","repo":"TheAlgorithms/Python","slug":"input-sequence-should-not-be-empty","errorCode":null,"errorMessage":"Input sequence should not be empty","messagePattern":"Input sequence should not be empty","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"other/maximum_subsequence.py","lineNumber":24,"sourceCode":"\n    Raises:\n      ValueError: when nums is empty.\n\n    >>> max_subsequence_sum([1,2,3,4,-2])\n    10\n    >>> max_subsequence_sum([-2, -3, -1, -4, -6])\n    -1\n    >>> max_subsequence_sum([])\n    Traceback (most recent call last):\n        . . .\n    ValueError: Input sequence should not be empty\n    >>> max_subsequence_sum()\n    Traceback (most recent call last):\n        . . .\n    ValueError: Input sequence should not be empty\n    \"\"\"\n    if nums is None or not nums:\n        raise ValueError(\"Input sequence should not be empty\")\n\n    ans = nums[0]\n    for i in range(1, len(nums)):\n        num = nums[i]\n        ans = max(ans, ans + num, num)\n\n    return ans\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n\n    # Try on a sample input from the user\n    n = int(input(\"Enter number of elements : \").strip())\n    array = list(map(int, input(\"\\nEnter the numbers : \").strip().split()))[:n]\n    print(max_subsequence_sum(array))","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/other/maximum_subsequence.py#L6-L42","documentation":"Raised by max_subsequence_sum when nums is None or an empty sequence. The algorithm seeds its running answer with nums[0], so at least one number is required; both a missing argument (default None) and an empty list are rejected.","triggerScenarios":"Calling max_subsequence_sum([]), max_subsequence_sum(None), or max_subsequence_sum() with no argument.","commonSituations":"Streaming or filtered data where an empty batch reaches the sum function, or calling with a default-None parameter that was never populated.","solutions":["Guard the call: if nums: total = max_subsequence_sum(nums) else: handle the empty case","Pass a non-empty list; even all-negative inputs are fine (e.g. [-2,-3,-1] returns -1)","If the empty case is valid in your domain, decide its semantics (0 or None) before calling"],"exampleFix":"# before\nbest = max_subsequence_sum(window)  # ValueError when window == []\n\n# after\nbest = max_subsequence_sum(window) if window else 0","handlingStrategy":"validation","validationCode":"def has_elements(nums) -> bool:\n    return nums is not None and len(nums) > 0","typeGuard":null,"tryCatchPattern":"try:\n    best = max_subsequence_sum(nums)\nexcept ValueError as e:\n    if 'should not be empty' in str(e):\n        best = 0  # define your own empty-sequence semantics\n    else:\n        raise","preventionTips":["Short-circuit empty batches/windows before calling the algorithm","Treat None defaults as 'no data' and skip the call"],"tags":["algorithm","input-validation","empty-input"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}