{"record":{"id":"3fbd3bf8979ac05c","repo":"TheAlgorithms/Python","slug":"source-voltage-must-be-positive-3fbd3b","errorCode":null,"errorMessage":"Source voltage must be positive.","messagePattern":"Source voltage must be positive\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/charging_inductor.py","lineNumber":84,"sourceCode":"\n    >>> charging_inductor(source_voltage=0,resistance=200,inductance=20,time=5)\n    Traceback (most recent call last):\n        ...\n    ValueError: Source voltage must be positive.\n\n    >>> charging_inductor(source_voltage=10,resistance=0,inductance=20,time=5)\n    Traceback (most recent call last):\n        ...\n    ValueError: Resistance must be positive.\n\n    >>> charging_inductor(source_voltage=15, resistance=25, inductance=0, time=5)\n    Traceback (most recent call last):\n        ...\n    ValueError: Inductance must be positive.\n    \"\"\"\n\n    if source_voltage <= 0:\n        raise ValueError(\"Source voltage must be positive.\")\n    if resistance <= 0:\n        raise ValueError(\"Resistance must be positive.\")\n    if inductance <= 0:\n        raise ValueError(\"Inductance must be positive.\")\n    return round(\n        source_voltage / resistance * (1 - exp((-time * resistance) / inductance)), 3\n    )\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":66,"sourceCodeEnd":98,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/charging_inductor.py#L66-L98","documentation":"Thrown by charging_inductor() in electronics/charging_inductor.py when source_voltage <= 0. The function computes the RL step response I(t) = (Vs/R)*(1 - e^(-Rt/L)) and requires a strictly positive source voltage; non-positive values are rejected as invalid for a charging model.","triggerScenarios":"charging_inductor(source_voltage=0, resistance=10, inductance=20, time=5) or any negative source_voltage. Note the parameter is named time (not time_sec).","commonSituations":"Unset config defaults of 0; swapping the positional order of source_voltage and time; feeding signed AC samples into a DC step-response model.","solutions":["Pass a strictly positive DC source voltage.","If modeling magnitude of a bipolar source, apply abs() deliberately at the call site.","Use keyword arguments (source_voltage=, resistance=, inductance=, time=) to avoid positional mix-ups."],"exampleFix":"# before\ncharging_inductor(source_voltage=0, resistance=10, inductance=20, time=5)\n\n# after\ncharging_inductor(source_voltage=12, resistance=10, inductance=20, time=5)","handlingStrategy":"validation","validationCode":"if source_voltage <= 0:\n    raise ValueError(\"source_voltage must be positive\")\ni = charging_inductor(source_voltage, resistance, inductance, time)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Note the parameter is named time, unlike charging_capacitor's time_sec.","This models a DC step, not AC — do not feed signed instantaneous samples."],"tags":["electronics","rl-circuit","physics","validation","python"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}