TheAlgorithms/Python · error · ValueError
Input series cannot have 0 as an element
Error message
Input series cannot have 0 as an element
What it means
Raised by is_harmonic_series() in maths/series/harmonic.py when any element of the input list equals 0. Harmonic series require reciprocals (1/x), and 1/0 is undefined, so zero elements make the test impossible. Unlike a wrong ratio (which returns False), zero is treated as invalid input and raises.
Source
Thrown at maths/series/harmonic.py:50
Traceback (most recent call last):
...
ValueError: Input series cannot have 0 as an element
>>> is_harmonic_series([1,2,0,6])
Traceback (most recent call last):
...
ValueError: Input series cannot have 0 as an element
"""
if not isinstance(series, list):
raise ValueError("Input series is not valid, valid series - [1, 2/3, 2]")
if len(series) == 0:
raise ValueError("Input list must be a non empty list")
if len(series) == 1 and series[0] != 0:
return True
rec_series = []
series_len = len(series)
for i in range(series_len):
if series[i] == 0:
raise ValueError("Input series cannot have 0 as an element")
rec_series.append(1 / series[i])
common_diff = rec_series[1] - rec_series[0]
for index in range(2, series_len):
if rec_series[index] - rec_series[index - 1] != common_diff:
return False
return True
def harmonic_mean(series: list) -> float:
"""
return the harmonic mean of series
>>> harmonic_mean([1, 4, 4])
2.0
>>> harmonic_mean([3, 6, 9, 12])
5.759999999999999
>>> harmonic_mean(4)
Traceback (most recent call last):View on GitHub (pinned to f5988cc097)
Solutions
- Filter zeros before calling only if they are noise: is_harmonic_series([x for x in series if x != 0])
- Validate for zeros first and handle them as a separate business case
- If zeros are meaningful, compute the harmonic property yourself with explicit zero handling
Example fix
# before
is_harmonic_series([1, 2, 0, 6]) # -> ValueError
# after
if 0 in series:
print('series contains 0; harmonic test not applicable')
else:
print(is_harmonic_series(series)) Defensive patterns
Strategy: validation
Validate before calling
if any(x == 0 for x in series):
print('cannot test harmonic series containing 0')
else:
print(is_harmonic_series(series)) Type guard
def has_no_zero(series: list) -> bool:
return all(x != 0 for x in series) Try / catch
try:
is_harmonic_series(series)
except ValueError as e:
if '0 as an element' in str(e):
# zeros are invalid input for this test
result = False Prevention
- Treat zeros in data as a distinct business case before math calls
- Remember [0] raises even though other single-element lists return True
- Filter zeros only when they are noise, never silently when meaningful
When it happens
Trigger: Calling is_harmonic_series([0]) or is_harmonic_series([1, 2, 0, 6]) — any list containing at least one 0. Note the [0] single-element case: the len==1 early-return only applies when series[0] != 0, so [0] still raises.
Common situations: Numerical data containing genuine zeros (sensor readings, counts); uninitialized arrays; check-and-skip logic upstream. If zeros are legitimate data, this function cannot be used as-is.
Related errors
- Input list must be a non empty list
- Input series is not valid, valid series - [1, 2/3, 2]
- Input list must be a non empty list
- Input series is not valid, valid series - [2, 4, 6]
- Length must be a positive integer.
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/84e422d082551d76.
Report an issue: GitHub.