From 5d0f62f74eeb798c838fddbdbffad367db75b39f Mon Sep 17 00:00:00 2001 From: nikhitha79 <115790230+nikhitha79@users.noreply.github.com> Date: Fri, 4 Oct 2024 13:06:59 +0530 Subject: [PATCH 1/2] Create ohms_law.py --- physics/ohms_law.py | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 physics/ohms_law.py diff --git a/physics/ohms_law.py b/physics/ohms_law.py new file mode 100644 index 000000000000..664bb202b23c --- /dev/null +++ b/physics/ohms_law.py @@ -0,0 +1,57 @@ +def ohms_law(current: float, resistance: float, decimals: int = 1) -> float: + """ + Calculate the voltage when current and resistance data is given using Ohm's Law. + + Ohm's Law states that: + V = I * R + + Where: + V = Voltage (Volts) + I = Current (Amperes) + R = Resistance (Ohms) + + Parameters: + current (float): The current in Amperes (A). + resistance (float): The resistance in Ohms (Ω). + decimals (int): The number of decimal places to round the voltage. Default is 1. + + Returns: + float: The calculated voltage in Volts (V). + + Examples: + >>> ohms_law(2.0, 5.0) + 10.0 + + >>> ohms_law(1.0, 4.0) + 4.0 + + >>> ohms_law(3, 0.5) + 1.5 + + >>> ohms_law(1.5, 2.0) + 3.0 + + # Test case for invalid input (zero resistance) + >>> ohms_law(2, 0) + Traceback (most recent call last): + ... + ValueError: Zero resistance + + # Test case for invalid input (zero current) + >>> ohms_law(0, 3) + Traceback (most recent call last): + ... + ValueError: Zero current + """ + if current == 0: + raise ValueError('Zero current') + if resistance == 0: + raise ValueError('Zero resistance') + + voltage = current * resistance + return round(voltage, decimals) + + +if __name__ == "__main__": + import doctest + doctest.testmod(verbose=True) From 9849c88562949e5157a18d25ffbb511cf14fede8 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 4 Oct 2024 07:37:54 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/ohms_law.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/physics/ohms_law.py b/physics/ohms_law.py index 664bb202b23c..2a03eb62fabf 100644 --- a/physics/ohms_law.py +++ b/physics/ohms_law.py @@ -4,39 +4,39 @@ def ohms_law(current: float, resistance: float, decimals: int = 1) -> float: Ohm's Law states that: V = I * R - + Where: V = Voltage (Volts) I = Current (Amperes) R = Resistance (Ohms) - + Parameters: current (float): The current in Amperes (A). resistance (float): The resistance in Ohms (Ω). decimals (int): The number of decimal places to round the voltage. Default is 1. - + Returns: float: The calculated voltage in Volts (V). - + Examples: >>> ohms_law(2.0, 5.0) 10.0 - + >>> ohms_law(1.0, 4.0) 4.0 - + >>> ohms_law(3, 0.5) 1.5 - + >>> ohms_law(1.5, 2.0) 3.0 - + # Test case for invalid input (zero resistance) >>> ohms_law(2, 0) Traceback (most recent call last): ... ValueError: Zero resistance - + # Test case for invalid input (zero current) >>> ohms_law(0, 3) Traceback (most recent call last): @@ -44,14 +44,15 @@ def ohms_law(current: float, resistance: float, decimals: int = 1) -> float: ValueError: Zero current """ if current == 0: - raise ValueError('Zero current') + raise ValueError("Zero current") if resistance == 0: - raise ValueError('Zero resistance') + raise ValueError("Zero resistance") - voltage = current * resistance + voltage = current * resistance return round(voltage, decimals) if __name__ == "__main__": import doctest + doctest.testmod(verbose=True)