{"record":{"id":"2647c403bf87b80d","repo":"TheAlgorithms/Python","slug":"inductance-cannot-be-0-or-negative","errorCode":null,"errorMessage":"Inductance cannot be 0 or negative","messagePattern":"Inductance cannot be 0 or negative","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/resonant_frequency.py","lineNumber":35,"sourceCode":"    \"\"\"\n    This function can calculate the resonant frequency of LC circuit,\n    for the given value of inductance and capacitnace.\n\n    Examples are given below:\n    >>> resonant_frequency(inductance=10, capacitance=5)\n    ('Resonant frequency', 0.022507907903927652)\n    >>> resonant_frequency(inductance=0, capacitance=5)\n    Traceback (most recent call last):\n      ...\n    ValueError: Inductance cannot be 0 or negative\n    >>> resonant_frequency(inductance=10, capacitance=0)\n    Traceback (most recent call last):\n      ...\n    ValueError: Capacitance cannot be 0 or negative\n    \"\"\"\n\n    if inductance <= 0:\n        raise ValueError(\"Inductance cannot be 0 or negative\")\n\n    elif capacitance <= 0:\n        raise ValueError(\"Capacitance cannot be 0 or negative\")\n\n    else:\n        return (\n            \"Resonant frequency\",\n            float(1 / (2 * pi * (sqrt(inductance * capacitance)))),\n        )\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":17,"sourceCodeEnd":51,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/resonant_frequency.py#L17-L51","documentation":"Raised by resonant_frequency() in electronics/resonant_frequency.py when inductance <= 0. The function computes f = 1/(2*pi*sqrt(L*C)), where a non-positive L makes the expression undefined (or a short/degenerate LC tank), so both L and C are required to be strictly positive; L is checked first.","triggerScenarios":"resonant_frequency(inductance=0, capacitance=5); passing a negative inductance such as -10; default-initialized variables (0.0) passed because a sensor or config value was never set.","commonSituations":"Simulation setups where a component is omitted and defaults to 0; parsing configs where the inductance key is missing and a helper returns 0; unit-scale mistakes (e.g. entering 0 because the value was expressed in a scaled unit elsewhere).","solutions":["Pass a strictly positive inductance in henries, e.g. resonant_frequency(inductance=10, capacitance=5).","Use keyword arguments so L and C cannot be silently swapped.","Validate config values at load time: `if cfg.inductance <= 0: raise` with a message naming the config key."],"exampleFix":"# before\nresonant_frequency(inductance=0, capacitance=5)  # ValueError\n\n# after\nresonant_frequency(inductance=10, capacitance=5)","handlingStrategy":"validation","validationCode":"if not (inductance > 0):\n    raise ConfigError(\"inductance must be > 0 henries\")","typeGuard":"def valid_lc(l: float, c: float) -> bool:\n    return isinstance(l, (int, float)) and isinstance(c, (int, float)) and l > 0 and c > 0","tryCatchPattern":"try:\n    name, freq = resonant_frequency(inductance=L, capacitance=C)\nexcept ValueError as exc:\n    raise SimulationConfigError(str(exc)) from exc","preventionTips":["Use keyword arguments for L and C to avoid swaps.","Fail fast on unset (0-default) config values at load time.","Keep SI units (henries/farads) end to end."],"tags":["electronics","lc-circuit","validation","physics"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}