Skip to content

Added bmi_converion.py #3727

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 3 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
41 changes: 41 additions & 0 deletions conversions/bmi_conversion.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
"""Converts height and mass to Body Mass Index"""


def metric_units_bmi(kg: int, cm: int) -> int:
"""
Convert mass in kilograms and height in centimetres to Body Mass Index
>>> metric_units_bmi(50, 160)
19.53
>>> metric_units_bmi(80, 175)
26.12
>>> metric_units_bmi(20, 0)
"Height couldn't be 0"
"""
if cm == 0:
return "Height couldn't be 0"

metre_square = (cm / 100) ** 2
return round(kg / metre_square, 2)


def us_units_bmi(pounds: int, inches: int) -> int:
"""
Convert mass in pounds and height in inches to Body Mass Index
>>> us_units_bmi(200, 75)
25.0
>>> us_units_bmi(130, 65)
21.63
>>> us_units_bmi(40, 0)
"Height couldn't be 0"
"""
if inches == 0:
return "Height couldn't be 0"

inches_square = inches ** 2
return round(703 * (pounds / inches_square), 2)


if __name__ == "__main__":
import doctest

doctest.testmod()