ahmet / Python-for-Data-Science
Herkese Açık Ahmet Kerem Piscine Final
give_bmi.py 1.7 KB
def give_bmi(height: list[int | float],
weight: list[int | float]) -> list[int | float]:
"""Compute BMI values from height and weight lists."""
if not isinstance(height, list) or not isinstance(weight, list):
raise TypeError("height and weight must be lists")
if len(height) != len(weight):
raise ValueError("Lists must have the same length")
for h, w in zip(height, weight):
if not isinstance(h, (int, float)) or isinstance(h, bool):
raise TypeError("Height values must be int or float")
if not isinstance(w, (int, float)) or isinstance(w, bool):
raise TypeError("Weight values must be int or float")
if h <= 0:
raise ValueError("Height must be positive")
return [w / (h * h) for w, h in zip(weight, height)]
def apply_limit(bmi: list[int | float], limit: int) -> list[bool]:
"""Return True for BMI values above the limit, False otherwise."""
if not isinstance(bmi, list):
raise TypeError("bmi must be a list")
if not isinstance(limit, int) or isinstance(limit, bool):
raise TypeError("Limit must be an int")
for b in bmi:
if not isinstance(b, (int, float)) or isinstance(b, bool):
raise TypeError("BMI values must be int or float")
return [b > limit for b in bmi]
def main() -> None:
"""Run a small demonstration of the two functions."""
try:
height = [2.71, 1.15]
weight = [165.3, 38.4]
bmi = give_bmi(height, weight)
print(bmi, type(bmi))
print(apply_limit(bmi, 26))
except (TypeError, ValueError) as error:
print(f"{type(error).__name__}: {error}")
if __name__ == "__main__":
main()