From 250fcd36d5304939febf35dfa7d9c01fff249db8 Mon Sep 17 00:00:00 2001 From: nikhitha79 <115790230+nikhitha79@users.noreply.github.com> Date: Fri, 4 Oct 2024 12:57:34 +0530 Subject: [PATCH 1/2] Create ohms_law.py --- physics/ohms_law.py | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 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..bddd190d72c3 --- /dev/null +++ b/physics/ohms_law.py @@ -0,0 +1,44 @@ +def ohms_law(current: float, resistance: float) -> float: + """ + Calculate the voltage when current and resistance data is given using Ohm's Law. + + Ohm's Law states that: + V = I * R + + V = Voltage (Volts) + I = Current (Amperes) + R = Resistance (Ohms) + + >>> ohms_law(2,5) + 10.0 + + >>> ohms_law(1,4) + 4.0 + + >>> ohms_law(3,0.5) + 1.5 + + >>> ohms_law(1.5,2) + 3.0 + + # Test case for invalid input + >>> ohms_law(2,0) + Traceback (most recent call last): + ... + ValueError: Zero resistance + + >>> ohms_law(0,3) + Traceback (most recent call last): + ... + ValueError: Zero current + """ + if current or resistance ==0 + raise ValueError('Current or Resistance cannot be zero') + + voltage = current * resistance + return voltage + + +if __name__ == "__main__": + import doctest + doctest.testmod(verbose=True) From fc7fe522415fce26064105f524baf8066cea217b 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:29:05 +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 | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/physics/ohms_law.py b/physics/ohms_law.py index bddd190d72c3..756ed5df429c 100644 --- a/physics/ohms_law.py +++ b/physics/ohms_law.py @@ -4,38 +4,38 @@ def ohms_law(current: float, resistance: float) -> float: Ohm's Law states that: V = I * R - + V = Voltage (Volts) I = Current (Amperes) R = Resistance (Ohms) - + >>> ohms_law(2,5) 10.0 - + >>> ohms_law(1,4) 4.0 - + >>> ohms_law(3,0.5) 1.5 - + >>> ohms_law(1.5,2) 3.0 - + # Test case for invalid input >>> ohms_law(2,0) Traceback (most recent call last): ... ValueError: Zero resistance - + >>> ohms_law(0,3) Traceback (most recent call last): ... ValueError: Zero current """ - if current or resistance ==0 + if current or resistance ==0 raise ValueError('Current or Resistance cannot be zero') - voltage = current * resistance + voltage = current * resistance return voltage