TheAlgorithms/Python · error · ValueError
Capacitor at index {index} has a negative value!
Error message
Capacitor at index {index} has a negative value! What it means
Raised by capacitor_parallel() (electronics/capacitor_equivalence.py:21) when a capacitor value in the list is negative. Capacitance is physically non-negative, and the function simply sums values (Ceq = C1+C2+...+Cn in parallel), so a negative entry is treated as bad data rather than mathematically continued. The message includes the offending index.
Source
Thrown at electronics/capacitor_equivalence.py:21
from __future__ import annotations
def capacitor_parallel(capacitors: list[float]) -> float:
"""
Ceq = C1 + C2 + ... + Cn
Calculate the equivalent resistance for any number of capacitors in parallel.
>>> capacitor_parallel([5.71389, 12, 3])
20.71389
>>> capacitor_parallel([5.71389, 12, -3])
Traceback (most recent call last):
...
ValueError: Capacitor at index 2 has a negative value!
"""
sum_c = 0.0
for index, capacitor in enumerate(capacitors):
if capacitor < 0:
msg = f"Capacitor at index {index} has a negative value!"
raise ValueError(msg)
sum_c += capacitor
return sum_c
def capacitor_series(capacitors: list[float]) -> float:
"""
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!
"""View on GitHub (pinned to f5988cc097)
Solutions
- Remove or correct the negative entry: the error message's index tells you exactly which element to fix.
- If -1 is your 'missing component' sentinel, filter it out before calling: [c for c in caps if c >= 0].
- Validate component values at parse time with a physical-range check (0 to, say, 1 farad).
Example fix
# before capacitor_parallel([5.71389, 12, -3]) # ValueError: index 2 # after capacitor_parallel([5.71389, 12, 3])
Defensive patterns
Strategy: validation
Validate before calling
def valid_capacitors(caps: list[float]) -> bool:
return all(c >= 0 for c in caps) Try / catch
try:
capacitor_parallel(caps)
except ValueError as e:
if 'negative' in str(e):
idx = int(e.args[0].split('index ')[1].split(' ')[0])
caps = [c for i, c in enumerate(caps) if i != idx]
else:
raise Prevention
- Filter sentinel -1 'missing' values from netlists before analysis.
- Physically range-check parsed component values (0 < C < 1 F).
- The error message's index pinpoints the bad element — read it.
When it happens
Trigger: Calling capacitor_parallel([5.71389, 12, -3]) exactly as in the doctest; any list where element i < 0 raises with that index. Zero values are accepted (they just contribute nothing). capacitor_series has its own analogous check.
Common situations: Netlist parsing where a missing component defaults to -1 as a sentinel; measurement data with sign-inverted readings; hand-entered parts lists with typos.
Related errors
- Donor concentration should be positive
- Acceptor concentration should be positive
- Intrinsic concentration should be positive
- Capacitor at index {index} has a negative or zero value!
- Expected a_coeffs to have {self.order + 1} elements for {sel
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/250f827fe57d2bbe.
Report an issue: GitHub.