Skip to content

Create ohms_law.py #11733

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions physics/ohms_law.py
Original file line number Diff line number Diff line change
@@ -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')

Check failure on line 36 in physics/ohms_law.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff

physics/ohms_law.py:35:33: SyntaxError: Expected ':', found newline

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An error occurred while parsing the file: physics/ohms_law.py

Traceback (most recent call last):
  File "/opt/render/project/src/algorithms_keeper/parser/python_parser.py", line 146, in parse
    reports = lint_file(
              ^^^^^^^^^^
libcst._exceptions.ParserSyntaxError: Syntax Error @ 37:1.
parser error: error at 36:8: expected one of !=, %, &, (, *, **, +, -, ., /, //, :, <, <<, <=, ==, >, >=, >>, @, [, ^, and, if, in, is, not, or, |

        raise ValueError('Current or Resistance cannot be zero')
                                                               ^

voltage = current * resistance
return voltage


if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
Loading