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 1 commit
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

Check failure on line 7 in physics/ohms_law.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

physics/ohms_law.py:7:1: W293 Blank line contains whitespace
V = Voltage (Volts)
I = Current (Amperes)
R = Resistance (Ohms)

Check failure on line 11 in physics/ohms_law.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

physics/ohms_law.py:11:1: W293 Blank line contains whitespace
>>> ohms_law(2,5)
10.0

Check failure on line 14 in physics/ohms_law.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

physics/ohms_law.py:14:1: W293 Blank line contains whitespace
>>> ohms_law(1,4)
4.0

Check failure on line 17 in physics/ohms_law.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

physics/ohms_law.py:17:1: W293 Blank line contains whitespace
>>> ohms_law(3,0.5)
1.5

Check failure on line 20 in physics/ohms_law.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

physics/ohms_law.py:20:1: W293 Blank line contains whitespace
>>> ohms_law(1.5,2)
3.0

Check failure on line 23 in physics/ohms_law.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

physics/ohms_law.py:23:1: W293 Blank line contains whitespace
# Test case for invalid input
>>> ohms_law(2,0)
Traceback (most recent call last):
...
ValueError: Zero resistance

Check failure on line 29 in physics/ohms_law.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W293)

physics/ohms_law.py:29:1: W293 Blank line contains whitespace
>>> ohms_law(0,3)
Traceback (most recent call last):
...
ValueError: Zero current
"""
if current or resistance ==0

Check failure on line 35 in physics/ohms_law.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

physics/ohms_law.py:35:33: W291 Trailing whitespace
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:34: 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

Check failure on line 38 in physics/ohms_law.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (W291)

physics/ohms_law.py:38:35: W291 Trailing whitespace
return voltage


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