{"record":{"id":"3e3e10f8b374f0ea","repo":"TheAlgorithms/Python","slug":"capacitor-at-index-index-has-a-negative-or-zero","errorCode":null,"errorMessage":"Capacitor at index {index} has a negative or zero value!","messagePattern":"Capacitor at index (.+?) has a negative or zero value!","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/capacitor_equivalence.py","lineNumber":45,"sourceCode":"    \"\"\"\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    \"\"\"\n\n    first_sum = 0.0\n    for index, capacitor in enumerate(capacitors):\n        if capacitor <= 0:\n            msg = f\"Capacitor at index {index} has a negative or zero value!\"\n            raise ValueError(msg)\n        first_sum += 1 / capacitor\n    return 1 / first_sum\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":27,"sourceCodeEnd":54,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/capacitor_equivalence.py#L27-L54","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"# before\ncapacitor_series([5.71389, 12, 0.000])  # ValueError\n\n# after\ncapacitor_series([5.71389, 12, 10.0])","handlingStrategy":"validation","validationCode":"def valid_capacitors(values):\n    return all(isinstance(c, (int, float)) and c > 0 for c in values)\n\nif not valid_capacitors(capacitors):\n    bad = [i for i, c in enumerate(capacitors) if c <= 0]\n    raise ValueError(f\"non-positive capacitor at indices {bad}\")\nresult = capacitor_series(capacitors)","typeGuard":null,"tryCatchPattern":"try:\n    ceq = capacitor_series(capacitors)\nexcept ValueError as e:\n    if \"negative or zero value\" in str(e):\n        # strip/repair bad elements, then retry once with clean data\n        raise\n    raise","preventionTips":["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."],"tags":["electronics","physics","validation","python","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}