-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Electric power #3976
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
Electric power #3976
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,34 @@ | ||||||||||||
# https://en.m.wikipedia.org/wiki/Electric_power | ||||||||||||
|
||||||||||||
|
||||||||||||
def electric_power(voltage: float, current: float, power: float) -> float: | ||||||||||||
""" | ||||||||||||
This function can calculate any one of the three (voltage, current, power), fundamental value of electrical system. | ||||||||||||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
examples are below: | ||||||||||||
>>> electric_power(voltage=0, current=2, power=5) | ||||||||||||
{'voltage': 2.5} | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
>>> electric_power(voltage=2, current=2, power=0) | ||||||||||||
{'power': 4.0} | ||||||||||||
>>> electric_power(voltage=-2, current=3, power=0) | ||||||||||||
{'power': 6.0} | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Need to test:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, should i also do this on ohms_law as well?? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's get this PR landed before you modify Ohm's law... Coincidentally, @rhettinger (who wrote the namedtuple) just tweeted about Ohm's law... |
||||||||||||
""" | ||||||||||||
if (voltage, current, power).count(0) != 1: | ||||||||||||
raise ValueError("Only one argument must be 0") | ||||||||||||
elif power < 0: | ||||||||||||
raise ValueError("Power cannot be negative in any electrical/electronics system") | ||||||||||||
elif voltage == 0: | ||||||||||||
return {'voltage': power / current} | ||||||||||||
elif current == 0: | ||||||||||||
return {'current': power / voltage} | ||||||||||||
elif power == 0: | ||||||||||||
# I created this variable because i want to remove negative sign from the power value | ||||||||||||
cclauss marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
result = (float(voltage * current)) | ||||||||||||
if result < 0: | ||||||||||||
result = result * -1 | ||||||||||||
return {'power': result} | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use the builtin function There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok |
||||||||||||
|
||||||||||||
|
||||||||||||
if __name__ == '__main__': | ||||||||||||
import doctest | ||||||||||||
|
||||||||||||
doctest.testmod() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.