{"record":{"id":"d7a512f72bcb64f2","repo":"TheAlgorithms/Python","slug":"resistor-at-index-index-has-a-negative-or-zero-v","errorCode":null,"errorMessage":"Resistor at index {index} has a negative or zero value!","messagePattern":"Resistor at index (.+?) has a negative or zero value!","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/resistor_equivalence.py","lineNumber":26,"sourceCode":"    Req = 1/ (1/R1 + 1/R2 + ... + 1/Rn)\n\n    >>> resistor_parallel([3.21389, 2, 3])\n    0.8737571620498019\n    >>> resistor_parallel([3.21389, 2, -3])\n    Traceback (most recent call last):\n        ...\n    ValueError: Resistor at index 2 has a negative or zero value!\n    >>> resistor_parallel([3.21389, 2, 0.000])\n    Traceback (most recent call last):\n        ...\n    ValueError: Resistor at index 2 has a negative or zero value!\n    \"\"\"\n\n    first_sum = 0.00\n    for index, resistor in enumerate(resistors):\n        if resistor <= 0:\n            msg = f\"Resistor at index {index} has a negative or zero value!\"\n            raise ValueError(msg)\n        first_sum += 1 / float(resistor)\n    return 1 / first_sum\n\n\ndef resistor_series(resistors: list[float]) -> float:\n    \"\"\"\n    Req = R1 + R2 + ... + Rn\n\n    Calculate the equivalent resistance for any number of resistors in parallel.\n\n    >>> resistor_series([3.21389, 2, 3])\n    8.21389\n    >>> resistor_series([3.21389, 2, -3])\n    Traceback (most recent call last):\n        ...\n    ValueError: Resistor at index 2 has a negative value!\n    \"\"\"\n    sum_r = 0.00","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/resistor_equivalence.py#L8-L44","documentation":"Raised by resistor_parallel() in electronics/resistor_equivalence.py when any element of the resistors list is <= 0. The formula computes 1/sum(1/Ri), and a zero resistance would divide by zero while a negative one is physically meaningless, so the function validates each element (using its 0-based list index in the message) before accumulating.","triggerScenarios":"resistor_parallel([3.21389, 2, 0.000]) or resistor_parallel([10, -4.7, 3]) — any zero or negative element, at the index named in the message; empty sensor reads that default to 0.0 fed straight into the call.","commonSituations":"Hardware measurements where a channel reads 0 before calibration; CSV rows with missing values parsed as 0; mixing up series/parallel helpers and passing an intended series list containing a placeholder 0.","solutions":["Filter or reject non-positive measurements at ingestion: `vals = [r for r in resistors if r > 0]` only if zero truly means 'no resistor present'.","Treat 0 as 'absent branch' and remove that resistor before calling, since an absent branch does not change a parallel combination.","Fix the upstream measurement/calibration so real resistances are positive.","Wrap in try/except ValueError and report the offending index to the operator."],"exampleFix":"# before\nr = resistor_parallel([3.21389, 2, 0.000])  # ValueError: index 2\n\n# after\nvals = [r for r in [3.21389, 2, 0.000] if r > 0]  # drop absent/shorted branches\nr = resistor_parallel(vals)","handlingStrategy":"validation","validationCode":"def clean_parallel_branches(resistors: list[float]) -> list[float]:\n    # 0-ohm branch = short -> invalid; treat 0 as absent branch only if that is your intent\n    bad = [i for i, r in enumerate(resistors) if r <= 0]\n    if bad:\n        raise UserInputError(f\"Non-positive resistance at indices {bad}\")\n    return resistors","typeGuard":"def all_positive(vals: list[float]) -> bool:\n    return all(isinstance(r, (int, float)) and r > 0 for r in vals)","tryCatchPattern":"try:\n    req = resistor_parallel(resistors)\nexcept ValueError as exc:\n    raise MeasurementError(f\"Bad channel: {exc}\") from exc","preventionTips":["Reject or drop zero readings from hardware at ingestion.","Distinguish 'absent branch' (drop it) from 'shorted branch' (hard error).","Log the offending index from the message when re-raising."],"tags":["electronics","resistor","parallel","validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}