roberthsu2003 / __2024_05_05_sunday__

AI 人工智慧開發入門_python
29 stars 2 forks source link

計算BMI(請使用自訂的function) #14

Open roberthsu2003 opened 2 months ago

roberthsu2003 commented 2 months ago

自訂的function:

輸入:

輸出:

截圖 2024-05-12 下午4 46 26 截圖 2024-05-19 下午3 30 25
eddie3256 commented 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} 範圍")

image

jonathan-sean commented 2 months ago

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: image

sanchangj commented 2 months ago
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)}")

螢幕擷取畫面 2024-05-28 163341

charlywang11 commented 2 months ago

計算BMI(請使用自訂的function)

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}")

image

mimionana2 commented 1 month ago
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)}")
螢幕擷取畫面 2024-06-01 202819 螢幕擷取畫面 2024-06-01 202858
OrbAkatsuki commented 1 month ago

### 修改使用自訂的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}")

image

shadow40744398 commented 1 month ago
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)}")

image

wjoker444116 commented 1 month ago
[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)

image

aaaasssddfghjkl commented 1 month ago

[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)``