Open roberthsu2003 opened 1 month ago
class BMI():
def __init__(self, n, h, w):
self.__name = n
self.__height = h / 100
self.__weight = w
@property
def name(self):
return self.__name
@property
def height(self):
return self.__height
@property
def weight(self):
return self.__weight
def bmi(self)->float:
return round(self.weight / self.height ** 2, 5)
def status(self)->str:
if self.bmi() < 18.5:
rate = "過輕"
elif self.bmi() < 24:
rate = "正常"
elif self.bmi() < 27:
rate = "過重"
elif self.bmi() < 30:
rate = "輕度肥胖"
elif self.bmi() < 35:
rate = "中度肥胖"
else:
rate = "重度肥胖"
return rate
import pyinputplus as pyip
from health import BMI
def main():
name = pyip.inputStr("請輸入您的姓名: ")
#print(name) #使用python終端機執行時會自動打上去
height = pyip.inputInt("請輸入您的身高(cm): ", min=50, max=250)
#rint(height)
weight = pyip.inputInt("請輸入您的體重(kg): ", min=0, max=200)
#print(weight)
data = BMI(name, height, weight)
print(f"{data.name}, 您的BMI值為 {data.bmi()}")
print(f"您的體重為 {data.status()}")
if __name__ == "__main__":
main()
Python code: health.py
# BMI 計算 (with function)
# BMI = weight(kg) / height(m)^2
class BMI:
@property
def name(self):
return self.__name
@name.setter
def name(self, name:str):
self.__name = name
@property
def height(self):
return self.__height
@height.setter
def height(self, height:float):
self.__height = height
@property
def weight(self):
return self.__weight
@weight.setter
def weight(self, weight:float):
self.__weight = weight
def bmi(self)->float:
return self.__weight / (self.__height / 100)**2
def status(self, bmi:float)->str:
if bmi < 18.5:
return "過輕"
elif bmi >= 18.5 and bmi < 24:
return "正常"
elif bmi >= 24 and bmi < 27:
return "過重"
elif bmi >= 27 and bmi < 30:
return "輕度肥胖"
elif bmi >= 30 and bmi < 35:
return "中度肥胖"
return "重度肥胖"
index.py
import pyinputplus as pyip
from health import BMI
def main():
INPUT_TRY = 3
print("BMI 計算")
# Exception handling refer to https://docs.python.org/3/tutorial/errors.html
try:
# Create class instance of BMI
obj = BMI()
# Input user information, include name, height and weight
obj.name = pyip.inputStr("請輸入姓名: ", limit=INPUT_TRY)
obj.height = pyip.inputFloat("請輸入身高(cm): ", min=0, limit=INPUT_TRY)
obj.weight = pyip.inputFloat("請輸入體重(kg): ", min=0, limit=INPUT_TRY)
# Calculate BMI and get result
bmi = obj.bmi()
status = obj.status(bmi)
# Output BMI and result
print(f"{obj.name} BMI 值為 {round(bmi, 2)}")
print(f"您的體重: {status}")
except Exception as e:
print(f"ERROR - {type(e)}")
if __name__ == '__main__':
main()
Result:
module名稱:health.py
class BMI():
def __init__(self,name:str,height:int,weight:int):
self.__name = name
self.__height = height
self.__weight = weight
@property
def name(self)->str:
return self.name
@property
def height(self)->int:
return self.height
@property
def weight(self)->int:
return self.weight
@classmethod
def cal_bmi(cls,height:int, weight:int)->float:
bmi = weight / (height / 100) ** 2
return bmi
@classmethod
def get_status(cls,bmi:float)->str:
if bmi < 18.5:
status = "過輕"
elif bmi < 24:
status = "正常"
elif bmi < 27:
status = "過重"
elif bmi < 30:
status = "輕度肥胖"
elif bmi < 35:
status = "中度肥胖"
else:
status = "重度肥胖"
return status
主程式index.py
import pyinputplus as pypi
from health import BMI
def main()->None:
name = pypi.inputStr("請輸入您的姓名:")
height = pypi.inputInt("請輸入您的身高(cm):",min=1)
weight = pypi.inputInt("請輸入您的體重(kg):",min=1)
bmi = BMI.cal_bmi(height, weight)
status = BMI.get_status(bmi)
print(f"{name} bmi值為:{round(bmi,3)}")
print(f"您的體重:{status}")
if __name__ == '__main__':
main()
health.py
class BMI():
def __init__(self,name:str,height:int,weight:int):
self.__name = name
self.__height = height
self.__weight = weight
@property
def name(self)->str:
return self.name
@property
def height(self)->int:
return self.height
@property
def weight(self)->int:
return self.weight
@classmethod
def cal_bmi(cls,height:int, weight:int)->float:
bmi = weight / (height / 100) ** 2
return bmi
@classmethod
def get_status(cls,bmi:float)->str:
if bmi < 18.5:
status = "過輕"
elif bmi < 24:
status = "正常"
elif bmi < 27:
status = "過重"
elif bmi < 30:
status = "輕度肥胖"
elif bmi < 35:
status = "中度肥胖"
else:
status = "重度肥胖"
return status
index.py
import pyinputplus as pypi
from health import BMI
def main()->None:
name = pypi.inputStr("請輸入您的姓名:")
height = pypi.inputInt("請輸入您的身高(cm):",min=1)
weight = pypi.inputInt("請輸入您的體重(kg):",min=1)
bmi = BMI.cal_bmi(height, weight)
status = BMI.get_status(bmi)
print(f"{name} bmi值為:{round(bmi,3)}")
print(f"您的體重:{status}")
if __name__ == '__main__':
main()
module名稱:health.py
class名稱:BMI
主程式index.py
from health import BMI