{"record":{"id":"b1df25c7e9ae2efe","repo":"TheAlgorithms/Python","slug":"input-series-is-not-valid-valid-series-2-4-6-b1df25","errorCode":null,"errorMessage":"Input series is not valid, valid series - [2, 4, 6]","messagePattern":"Input series is not valid, valid series - \\[2, 4, 6\\]","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/series/harmonic.py","lineNumber":80,"sourceCode":"\n    >>> harmonic_mean([1, 4, 4])\n    2.0\n    >>> harmonic_mean([3, 6, 9, 12])\n    5.759999999999999\n    >>> harmonic_mean(4)\n    Traceback (most recent call last):\n        ...\n    ValueError: Input series is not valid, valid series - [2, 4, 6]\n    >>> harmonic_mean([1, 2, 3])\n    1.6363636363636365\n    >>> harmonic_mean([])\n    Traceback (most recent call last):\n        ...\n    ValueError: Input list must be a non empty list\n\n    \"\"\"\n    if not isinstance(series, list):\n        raise ValueError(\"Input series is not valid, valid series - [2, 4, 6]\")\n    if len(series) == 0:\n        raise ValueError(\"Input list must be a non empty list\")\n    answer = 0\n    for val in series:\n        answer += 1 / val\n    return len(series) / answer\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":62,"sourceCodeEnd":93,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/series/harmonic.py#L62-L93","documentation":"Raised by harmonic_mean() in maths/series/harmonic.py when the argument is not a list instance. The function sums reciprocals 1/val over the elements, requiring an indexable Python list of numbers. The '[2, 4, 6]' in the message is just a sample of a valid input, which can mislead developers into thinking their values are wrong when actually the type is wrong.","triggerScenarios":"Calling harmonic_mean(3), harmonic_mean('123'), harmonic_mean((1,2,3)), or passing any non-list object. This isinstance check runs before the emptiness check, so non-list empties (like empty tuple) fail here too.","commonSituations":"Passing tuples or other iterables from libraries with different conventions; forgetting to .split() or json.loads a raw input string; numpy arrays not converted with .tolist().","solutions":["Coerce to list: harmonic_mean(list(values))","Convert numpy arrays: harmonic_mean(arr.tolist())","Parse string input into a list of floats before calling"],"exampleFix":"# before\nharmonic_mean((1, 2, 3))  # tuple -> ValueError\n\n# after\nfrom statistics import harmonic_mean as st_hm  # or:\nprint(harmonic_mean([1, 2, 3]))  # -> 1.6363...","handlingStrategy":"type-guard","validationCode":"if not isinstance(series, list):\n    series = [float(x) for x in series]\nprint(harmonic_mean(series))","typeGuard":"def is_float_list(value: object) -> bool:\n    return isinstance(value, list) and all(\n        isinstance(x, (int, float)) and x != 0 for x in value\n    )","tryCatchPattern":null,"preventionTips":["The '[2, 4, 6]' in the message is a sample; the real issue is type","Convert numpy arrays with .tolist()","Also guard zeros separately — harmonic_mean([0]) raises ZeroDivisionError"],"tags":["python","math","type-check","value-error"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}