roberthsu2003 / __2024_04_17_mon_wed__

Python與AI人工智慧開發入門
25 stars 4 forks source link

計算BMI, #10

Open roberthsu2003 opened 1 month ago

roberthsu2003 commented 1 month ago

輸入:

請使用自訂的function

輸出:

錯誤:

截圖 2024-05-01 晚上9 28 27
chesterXalan commented 1 month ago
import pyinputplus as pyip

def calc_bmi(height: float, weight: float) -> float:
    return weight / (height / 100) ** 2

def get_status(bmi: float) -> str:
    if bmi < 18.5:
        result = '過輕'
    elif bmi < 24:
        result = '正常'
    elif bmi < 27:
        result = '過重'
    elif bmi < 30:
        result = '輕度肥胖'
    elif bmi < 35:
        result = '中度肥胖'
    else:
        result = '重度肥胖'
    return result

name = input('請輸入姓名: ')
height = pyip.inputFloat('請輸入身高(120~230)(cm): ', min=120, max=230)
weight = pyip.inputFloat('請輸入體重(40~170)(kg): ', min=40, max=170)

bmi = calc_bmi(height, weight)
status = get_status(bmi)

print(f'\n{name}, 您的BMI: {bmi:.2f}')
print(f'您的體重: {status}')

螢幕擷取畫面 2024-05-13 214354

MurrayChuang commented 1 month ago
import pyinputplus as pyip

def bmi(weight: float, height: float) -> float:
    return weight / (height / 100) ** 2

def get_status(bmi: float) -> str:
    if bmi < 18.5:
        result = '過輕'
    elif bmi < 24:
        result = '正常'
    elif bmi < 27:
        result = '過重'
    elif bmi < 30:
        result = '輕度肥胖'
    elif bmi < 35:
        result = '中度肥胖'
    else:
        result = '重度肥胖'
    return result

name = input('請輸入姓名:')
height = pyip.inputFloat('請輸入身高(cm):', min = 1, max = 300) 
weight = pyip.inputFloat('請輸入體重(kg):', min = 1, max = 500) 
bmi_value = bmi(weight, height)
print(f'{name}, 您的BMI: {bmi_value:.2f}')
print(f'{name}, 您的體重: {get_status(bmi_value)}')
image
Tony840705 commented 1 month ago
import pyinputplus as pyip

def bmi(weight: float, height: float) -> float:
    return weight / (height / 100) ** 2
def get_status(bmi: float) -> str:
    if bmi < 18.5:
        result = '體重過輕'
    elif bmi < 24:
        result = '體重正常'
    elif bmi < 27:
        result = '體重過重'
    elif bmi < 30:
        result = '輕度肥胖'
    elif bmi < 35:
        result = '中度肥胖'
    else:
        result = '重度肥胖'
    return result

try:
    name = input('請輸入姓名:');
    height = pyip.inputFloat('請輸入身高(120~230)(cm):', min = 120, max = 230)
    weight = pyip.inputFloat('請輸入體重(40~170)(kg):', min = 40, max = 170)
    bmi_value = bmi(weight, height)
    print(f'您的身高為{height}公分')
    print(f'您的體重為{weight}公斤')
    print(f'{name}, 您的BMI為:{bmi_value:.2f}')
    print(f'{name}, 您屬於:{get_status(bmi_value)}')

except Exception as e:
    print(f'格式錯誤:{e}')

BMI

chihweihan commented 1 month ago
import pyinputplus as pyip

name = pyip.inputStr("請輸入姓名:")
height = pyip.inputFloat("請輸入身高(cm):")
weight = pyip.inputFloat("糗輸入體重(kg):")

def bmi(height:float,weight:float):
    return weight / (height / 100) ** 2

def get_status(bmi:float):
    if bmi < 18.5:
        result = "體重過輕"
    elif bmi < 24:
        result = "正常範圍"
    elif bmi < 27:
        result = "體重過重"
    elif bmi < 30:
        result = "輕度肥胖"
    elif bmi < 35:
        result = "中度肥胖"
    else:
        result = "重度肥胖"
    return result

BMI = bmi(height,weight)
print(f"{name},您的BMI:{BMI}")
print(f"{name},您屬於:{get_status(BMI)}")

image

chiayuben commented 1 month ago
import pyinputplus as pyip

def bmi (high:float,weight:float)->float:
    return float(weight / (high / 100) ** 2)
def status(bmi: float|int) -> str:
    if bmi<18.5 :
        result='體重過輕'
    elif bmi>=18.5 and bmi<24:
        result='BMI正常'
    elif bmi>=24 and bmi<27:
        result='過重'
    elif bmi<=27 and bmi<30:
        result='輕度肥胖'
    elif bmi<=30 and bmi<35:
        result='中度肥胖'
    else:
       result='重度肥胖'
    return result

def BMT_count():
    name = input('請輸入姓名:')
    high = pyip.inputFloat('請輸入身高(50~230cm)',min=50,max=230)
    print(high)
    weight=pyip.inputFloat('請輸入體重(30~250Kg)',min=30,max=250)
    print(weight)
    BMI=bmi(high,weight)
    status(BMI)
    print(f'你的身高為{high},體重為{weight},你的BMI為{BMI:.2f},你的狀態為:{status(BMI)}')

while True:
    BMT_count()
    play_again=pyip.inputYesNo('還要下一位量測嗎?(y,n)\n')
    if play_again=='no':
        break
print('量測結束\n')    

image

Tina54321 commented 1 month ago
import pyinputplus as py
a = 0
b = 0

def bmi(a: float | int, b: float | int) -> float | int:
    a = py.inputFloat('\n請輸入身高(cm):', greaterThan=0); print(f'{a}', end=' ')
    b = py.inputNum('\n請輸入體重(kg):', greaterThan=0); print(f'{b}', end=' ')
    return b / (0.01 * a) ** 2

def get_status(bmi: float | int) -> str:
    if bmi < 18.5:
        result = '過輕'
    elif bmi < 24:
        result = '正常'
    elif bmi < 27:
        result = '過重'
    elif bmi < 30:
        result = '輕度肥胖'
    elif bmi < 35:
        result = '中度肥胖'
    else:
        result = '重度肥胖'
    return result

name = py.inputStr("請輸入姓名:"); print(f'{name}', end=' ')
BMI = bmi(a,b)
status = get_status(BMI)
print(f'\n{name},您的BMI:{BMI:.2f},為{status}')

image

ccanna commented 1 month ago
import pyinputplus as pyip
def bmi(weight:float, height:float)->float:
        return round(weight / (height/100)**2, ndigits=2)

def get_status(bmi:float)->str:
    if bmi <18.5:
        remind = '體重過輕'
    elif bmi >=18.5 and bmi <24:
        remind = '正常體重'
    elif bmi >=24 and bmi <27:
        remind = '體重過重'
    elif bmi >=27 and bmi <30:
        remind = '輕度肥胖'
    elif bmi >=30 and bmi <35:
        remind = '中度肥胖'
    else:
        remind = '重度肥胖'
    return remind
name = pyip.inputStr("請輸入姓名:")
height = pyip.inputFloat("請輸入身高(cm):")
weight = pyip.inputFloat("請輸入體重(kg):")

bmi_count= bmi(weight, height)

print(f"{name},您的BMI值為:{bmi_count}")

print(f"您目前:{get_status(bmi_count)}")

image

KIOVER998 commented 1 month ago
import pyinputplus as pyip

def calc_bmi(height: float, weight: float) -> float:
    return weight / (height / 100) ** 2

def get_status(bmi: float) -> str:
    if bmi < 18.5:
        result = '過輕'
    elif bmi < 24:
        result = '正常'
    elif bmi < 27:
        result = '過重'
    elif bmi < 30:
        result = '輕度肥胖'
    elif bmi < 35:
        result = '中度肥胖'
    else:
        result = '重度肥胖'
    return result

name:str = input('請輸入姓名: ')
height:float = pyip.inputFloat('請輸入身高(120~230)(cm): ', min=120, max=230)
weight:float = pyip.inputFloat('請輸入體重(40~170)(kg): ', min=40, max=170)

bmi:float = calc_bmi(height, weight)
status:str = get_status(bmi)

print(f'\n{name}, 您的BMI: {bmi:.2f}')
print(f'您的體重: {status}')

擷取

victor1629 commented 1 month ago
def calc_bmi(height:float,weight:float)->float:
        return weight / (height / 100) ** 2
def get_status (bmi:float)->str:
          if   bmi < 18.5:
               result="過輕"
          elif bmi <24:                  
               result="正常範圍"
          elif bmi <27:
               result="過重"
          elif bmi <30:
               result="輕度肥胖"
          elif bmi <35:
               result="中度肥胖"
          else: 
               bmi >=35
               result="重度肥胖"
          return result
import pyinputplus as pyip              

name = input('請輸入姓名:')
height = pyip.inputFloat('請輸入身高(120~230)(cm):',min=120,max=230)
print(f'您的身高{height}')
weight = pyip.inputFloat('請輸入體重(40~170)(kg):',min=40,max=170)
print(f'您的體重{weight}')

bmi:float=calc_bmi(height,weight)
status:str=get_status(bmi)

print(f'{name}, 您的BMI:{bmi:.2f}')
print(f'{name}, 您的體重:{status}')

螢幕擷取畫面 2024-05-19 153953

PercJK commented 1 month ago
import pyinputplus as pyip

def calc_bmi(height: float, weight: float) -> float:
    return weight / (height / 100) ** 2

def get_status(bmi: float) -> str:
    if bmi < 18.5:
        result = '過輕'
    elif bmi < 24:
        result = '正常'
    elif bmi < 27:
        result = '過重'
    elif bmi < 30:
        result = '輕度肥胖'
    elif bmi < 35:
        result = '中度肥胖'
    else:
        result = '重度肥胖'
    return result

name:str = input('請輸入姓名: ')
height:float = pyip.inputFloat('請輸入身高(120~230)(cm): ', min=120, max=230)
weight:float = pyip.inputFloat('請輸入體重(40~170)(kg): ', min=40, max=170)

bmi:float = calc_bmi(height, weight)
status:str = get_status(bmi)

print(f'\n{name}, 您的BMI: {bmi:.2f}')
print(f'您的體重: {status}')

lesson8 HW