TheAlgorithms/Python · error · ValueError
Capacitor at index {index} has a negative or zero value!
Error message
Capacitor at index {index} has a negative or zero value! What it means
Thrown by capacitor_series() in electronics/capacitor_equivalence.py when any element of the capacitors iterable is <= 0. The series-equivalent formula computes 1/C_eq = sum(1/C_i), which is undefined for zero and physically meaningless for negative capacitance, so the library rejects the input up front. The message includes the offending index so you can locate the bad element.
Source
Thrown at electronics/capacitor_equivalence.py:45
"""
Ceq = 1/ (1/C1 + 1/C2 + ... + 1/Cn)
>>> capacitor_series([5.71389, 12, 3])
1.6901062252507735
>>> capacitor_series([5.71389, 12, -3])
Traceback (most recent call last):
...
ValueError: Capacitor at index 2 has a negative or zero value!
>>> capacitor_series([5.71389, 12, 0.000])
Traceback (most recent call last):
...
ValueError: Capacitor at index 2 has a negative or zero value!
"""
first_sum = 0.0
for index, capacitor in enumerate(capacitors):
if capacitor <= 0:
msg = f"Capacitor at index {index} has a negative or zero value!"
raise ValueError(msg)
first_sum += 1 / capacitor
return 1 / first_sum
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Inspect the index reported in the message and fix or remove that element before calling capacitor_series.
- Filter non-positive values only if they are known to be sentinel/missing data: [c for c in capacitors if c > 0].
- Add a validation step upstream where the values enter your program (parser, config loader) so 0/negative never reaches the library.
Example fix
# before capacitor_series([5.71389, 12, 0.000]) # ValueError # after capacitor_series([5.71389, 12, 10.0])
Defensive patterns
Strategy: validation
Validate before calling
def valid_capacitors(values):
return all(isinstance(c, (int, float)) and c > 0 for c in values)
if not valid_capacitors(capacitors):
bad = [i for i, c in enumerate(capacitors) if c <= 0]
raise ValueError(f"non-positive capacitor at indices {bad}")
result = capacitor_series(capacitors) Try / catch
try:
ceq = capacitor_series(capacitors)
except ValueError as e:
if "negative or zero value" in str(e):
# strip/repair bad elements, then retry once with clean data
raise
raise Prevention
- Validate component lists at the data-ingestion boundary (CSV/parser), not at the physics call.
- Never use 0 as a 'missing value' sentinel for capacitance.
- Assert unit consistency (all farads) before building the list.
When it happens
Trigger: capacitor_series([5.71389, 12, 0.000]) or any call where an element is 0 or negative, e.g. capacitor_series([-4.7, 10]). Passing an empty-derived value or a computed list containing 0.0 also triggers it.
Common situations: Feeding parsed component values from a datasheet/CSV where a missing entry defaulted to 0; unit-conversion bugs producing 0 (e.g. uF vs F mix-up); test fixtures generated with range(0) style values.
Related errors
- You cannot supply more or less than 2 values
- Source voltage must be positive.
- Source voltage must be positive.
- Donor concentration should be positive
- Acceptor concentration should be positive
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/3e3e10f8b374f0ea.
Report an issue: GitHub.