Open roberthsu2003 opened 2 months ago
import pyinputplus as pypi
def cal_bmi(height:int, weight:int)->float:
BMI = weight / (height / 100) ** 2
return BMI
def get_status(bmi:float)->str:
if BMI < 18.5:
rate = "過輕"
elif BMI < 24:
rate = "正常"
elif BMI < 27:
rate = "過重"
elif BMI < 30:
rate = "輕度肥胖"
elif BMI < 35:
rate = "中度肥胖"
else:
rate = "重度肥胖"
return rate
name = pypi.inputStr("請輸入您的姓名: ")
print(name)
height = pypi.inputInt("請輸入您的身高(cm): ", min=50, max=250)
print(height)
weight = pypi.inputInt("請輸入您的體重(kg): ", min=0, max=200)
print(weight)
BMI = cal_bmi(height=height, weight=weight)
rate = get_status(BMI)
print(f"您的姓名為 {name}\n您的BMI值為 {BMI}\n您屬於 {rate} 範圍")
Python code:
# BMI 計算 (with function)
# BMI = weight(kg) / height(m)^2
import pyinputplus as pyip
def cal_bmi(height:float, weight:float)->float:
return weight / (height / 100)**2
def get_status(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 "重度肥胖"
INPUT_TRY = 3
print("BMI 計算,請輸入")
# Exception handling refer to https://docs.python.org/3/tutorial/errors.html
try:
# Input user information, include name, height and weight
name = pyip.inputStr("姓名: ", limit=INPUT_TRY)
height = pyip.inputFloat("身高(公分): ", min=0, limit=INPUT_TRY)
weight = pyip.inputFloat("體重(公斤): ", min=0, limit=INPUT_TRY)
# Calculate BMI and get result
bmi = cal_bmi(height, weight)
status = get_status(bmi)
# Output user information, BMI and result
print(f"\n{name} - \n身高: {height}cm\n體重: {weight}Kg")
print(f"BMI: {round(bmi, 2)} 體重{status}")
except Exception as e:
print(f"EXCEPTION - {type(e)}")
Result:
def cal_bmi(height:int,weight:int) -> float:
return weight/(height/100)**2
def get_status(BMI:float) -> str:
if BMI < 18.5:
return "你的體重過輕"
elif BMI < 24:
return "你的體重正常"
elif BMI < 27:
return "你的體重過重"
elif BMI < 30:
return "你的體重輕度肥胖"
elif BMI < 35:
return "你的體重中度肥胖"
else:
return "你的體重種度肥胖"
import pyinputplus as pypi
name:str=input("請輸入姓名:")
print(f"請輸入姓名:{name}")
height:int= pypi.inputFloat("請輸入身高(cm):")
print(height)
weight:int= pypi.inputFloat("請輸入體重(kg):")
print(weight)
BMI:float = cal_bmi(height,weight)
print(f"你的BMI:{round(BMI,ndigits=2)}")
print(f"{get_status(BMI)}")
def cal_bmi(height:int,weight:int) -> float:
bmi = weight / (height/100) ** 2
return bmi
def get_status(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
import pyinputplus as pypi
name = pypi.inputStr("請輸入您的姓名:")
print(name)
height = pypi.inputInt("請輸入您的身高(cm):",min=1)
print(height)
weight = pypi.inputInt("請輸入您的體重(kg):",min=1)
print(weight)
bmi = cal_bmi(height,weight)
status = get_status(bmi)
print("======================")
print(f"您的姓名是:{name}")
print(f"您的BMI值:{round(bmi, ndigits=2)}")
print(f"您的體重:{status}")
import pyinputplus as pypi
def cal_bmi(height:int,weight:int) -> float:
bmi = weight/(hight/100)**2
return bmi
def get_status(bmi:float)->str:
if bmi<18.5:
return "過輕"
elif bmi<24:
return "正常"
elif bmi<27:
return "過重"
elif bmi<30:
return "輕度肥胖"
elif bmi<35:
return "中度肥胖"
else:
return "重度肥胖"
name:str=input("請輸入姓名:")
print(f"請輸入姓名:{name}")
height:int=pypi.inputInt("請輸入身高cm:")
print (hight)
weight:int=pypi.inputInt("請輸入體重kg:")
print (weight)
bmi:float=cal_bmi(height,weight)
print(f"您的bmi值:{round(bmi,ndigits=2)}")
print(f"您的身體質量指數:{get_status(bmi)}")
### 修改使用自訂的function
import pyinputplus as pypi
name = pypi.inputStr("請輸入您的姓名: ")
print(name)
cm = pypi.inputInt("請輸入您的身高(cm): ",min=0,max=300)
print(cm)
kg = pypi.inputInt("請輸入您的體重(kg): ",min=0,max=300)
print(kg)
def cal_bmi(cm:int, kg:int):
bmi = kg / (cm/100) ** 2
return bmi
def get_status(bmi:float):
if bmi >= 35:
result = "重度肥胖"
elif bmi >= 30:
result = "中度肥胖"
elif bmi >= 27:
result = "輕度肥胖"
elif bmi >= 24:
result = "過重"
elif bmi >= 18.5:
result = "正常"
else:
result = "過輕"
return result
bmi = cal_bmi(cm=cm, kg=kg)
result = get_status(bmi=bmi)
print(f"您的姓名是: {name}")
print(f"您的BMI值: {round(bmi,ndigits=0)}")
print(f"您的體重: {result}")
import pyinputplus as pyip
def cal_bmi(height:int,weight:int) -> float:
return(round(weight / ((height/100)**2),ndigits=2))
def get_status(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
try:
name = pyip.inputStr("請輸入你的名字:")
height = pyip.inputFloat("請輸入你的身高(cm):",min=0,max=250)
weight = pyip.inputFloat("請輸入你的體重(kg):",min=0,max=200)
bmi = cal_bmi(height, weight)
status = get_status(bmi)
print(f"\n{name} \n身高: {height}cm\n體重: {weight}Kg")
print(f"BMI: {round(bmi, 2)} 你屬於{status}範圍")
except Exception as e:
print(f"EXCEPTION - {type(e)}")
[import pyinputplus as pypi
name = pypi.inputStr("請輸入您的姓名: ")
print(name)
cm = pypi.inputInt("請輸入您的身高(cm): ",min=0,max=300)
print(cm)
kg = pypi.inputInt("請輸入您的體重(kg): ",min=0,max=300)
print(kg)
def cal_bmi(cm:int, kg:int):
bmi = kg / (cm/100) ** 2
return bmi
def get_status(bmi:float):
if bmi >= 35:
result = "重度肥胖"
elif bmi >= 30:
result = "中度肥胖"
elif bmi >= 27:
result = "輕度肥胖"
elif bmi >= 24:
result = "過重"
elif bmi >= 18.5:
result = "正常"
else:
result = "過輕"
return result
bmi = cal_bmi(cm=cm, kg=kg)
result = get_status(bmi=bmi)
print(f"您的姓名是: {name}")
print(f"您的BMI值: {round(bmi,ndigits=0)}")
print(f"您的體重: {result}")](url)
[import pyinputplus as pypi
name = pypi.inputStr("請輸入您的姓名: ") print(name) cm = pypi.inputInt("請輸入您的身高(cm): ",min=0,max=300) print(cm) kg = pypi.inputInt("請輸入您的體重(kg): ",min=0,max=300) print(kg)
def cal_bmi(cm:int, kg:int): bmi = kg / (cm/100) ** 2 return bmi def get_status(bmi:float): if bmi >= 35: result = "重度肥胖" elif bmi >= 30: result = "中度肥胖" elif bmi >= 27: result = "輕度肥胖" elif bmi >= 24: result = "過重" elif bmi >= 18.5: result = "正常" else: result = "過輕" return result
bmi = cal_bmi(cm=cm, kg=kg) result = get_status(bmi=bmi)
print(f"您的姓名是: {name}") print(f"您的BMI值: {round(bmi,ndigits=0)}") print(f"您的體重: {result}")](url)``
自訂的function:
輸入:
輸出: