{"record":{"id":"250f827fe57d2bbe","repo":"TheAlgorithms/Python","slug":"capacitor-at-index-index-has-a-negative-value","errorCode":null,"errorMessage":"Capacitor at index {index} has a negative value!","messagePattern":"Capacitor at index (.+?) has a negative value!","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/capacitor_equivalence.py","lineNumber":21,"sourceCode":"from __future__ import annotations\n\n\ndef capacitor_parallel(capacitors: list[float]) -> float:\n    \"\"\"\n    Ceq = C1 + C2 + ... + Cn\n    Calculate the equivalent resistance for any number of capacitors in parallel.\n    >>> capacitor_parallel([5.71389, 12, 3])\n    20.71389\n    >>> capacitor_parallel([5.71389, 12, -3])\n    Traceback (most recent call last):\n        ...\n    ValueError: Capacitor at index 2 has a negative value!\n    \"\"\"\n    sum_c = 0.0\n    for index, capacitor in enumerate(capacitors):\n        if capacitor < 0:\n            msg = f\"Capacitor at index {index} has a negative value!\"\n            raise ValueError(msg)\n        sum_c += capacitor\n    return sum_c\n\n\ndef capacitor_series(capacitors: list[float]) -> float:\n    \"\"\"\n    Ceq = 1/ (1/C1 + 1/C2 + ... + 1/Cn)\n    >>> capacitor_series([5.71389, 12, 3])\n    1.6901062252507735\n    >>> capacitor_series([5.71389, 12, -3])\n    Traceback (most recent call last):\n        ...\n    ValueError: Capacitor at index 2 has a negative or zero value!\n    >>> capacitor_series([5.71389, 12, 0.000])\n    Traceback (most recent call last):\n        ...\n    ValueError: Capacitor at index 2 has a negative or zero value!\n    \"\"\"","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/capacitor_equivalence.py#L3-L39","documentation":"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.","triggerScenarios":"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.","commonSituations":"Netlist parsing where a missing component defaults to -1 as a sentinel; measurement data with sign-inverted readings; hand-entered parts lists with typos.","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)."],"exampleFix":"# before\ncapacitor_parallel([5.71389, 12, -3])  # ValueError: index 2\n\n# after\ncapacitor_parallel([5.71389, 12, 3])","handlingStrategy":"validation","validationCode":"def valid_capacitors(caps: list[float]) -> bool:\n    return all(c >= 0 for c in caps)","typeGuard":null,"tryCatchPattern":"try:\n    capacitor_parallel(caps)\nexcept ValueError as e:\n    if 'negative' in str(e):\n        idx = int(e.args[0].split('index ')[1].split(' ')[0])\n        caps = [c for i, c in enumerate(caps) if i != idx]\n    else:\n        raise","preventionTips":["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."],"tags":["electronics","input-validation","physics"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}