Create a Python program to track users' health data. Implement a HealthData class with attributes like name, age, and weight. Use a constructor to initialize these attributes when creating health data objects and calculate bmi(body mass index)(weight / height(meter) **2).
Code:
class HealthData:
def __init__(self, name, age, weight, height):
self.name = name
self.age = age
self.weight = weight
self.height = height
def calculate_bmi(self):
return self.weight / (self.height ** 2)
# Function to prompt the user to enter their health data
def enter_health_data():
name = input("Enter your name: ")
age = int(input("Enter your age: "))
weight = float(input("Enter your weight in kilograms: "))
height = float(input("Enter your height in meters: "))
return name, age, weight, height
# Function to display health data and BMI
def display_health_data(person):
print("\nHealth Data:")
print("Name:", person.name)
print("Age:", person.age)
print("Weight:", person.weight, "kg")
print("Height:", person.height, "m")
print("BMI:", person.calculate_bmi())
# Main function
def main():
# Prompt the user to enter health data
name, age, weight, height = enter_health_data()
# Create a HealthData object
user_health_data = HealthData(name, age, weight, height)
# Display health data and BMI
display_health_data(user_health_data)
if __name__ == "__main__":
main()
0 Comments