roberthsu2003 / __2024_08_31_chihlee_pico__

致理pico課程
MIT License
33 stars 2 forks source link

輸入身高,體重,計算BMI #2

Open roberthsu2003 opened 2 months ago

roberthsu2003 commented 2 months ago

輸入

輸出

截圖 2024-09-07 下午4 17 21
roberthsu2003 commented 2 months ago
value = 59
if value >= 90:
    print("90以上區段")
elif value >= 80:    
    print(">=80,<90")
elif value >= 70:
    print("<80,>=70")
elif value >=60:
    print("<70,>=60")
else:
    print("<60")
DrWolf0701 commented 2 months ago
try:  
    weight = int(input("請輸入體重(公斤):"))
    height = int(input("請輸入身高(公分):"))

    bmi = weight / ((height/100) ** 2)

    print(f"BMI是:{bmi:.2f}")

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

except ValueError:
    print("輸入格式有錯")

except Exception as e:
    print(f'錯誤訊息:{e}')

print("應用程式結束")
jasonSOUI commented 2 months ago
from decimal import Decimal, ROUND_HALF_UP

# https://depart.femh.org.tw/dietary/3OPD/BMI.htm
def getBMI(height, weight):

    try:
        # 公分轉公尺
        _height = height * Decimal(0.01) 

        # BMI = 體重(公斤) / 身高2(公尺2)
        bmi = weight / (_height ** 2) 
        bmi = bmi.quantize(Decimal('0.1'), rounding=ROUND_HALF_UP)

        # 狀態判斷
        status = ""
        if bmi >= 35:               # 重度肥胖:BMI≧35
            status = "重度肥胖"
        elif bmi >= 30:             # 中度肥胖:30≦BMI<35
            status = "中度肥胖"
        elif bmi >= 27 :            # 輕度肥胖:27≦BMI<30
             status = "輕度肥胖"  
        elif bmi >= 24:             # 過重:24≦BMI<27
            status = "過重"        
        elif bmi < 18.5:             # 體重過輕 BMI<18.5
            status = "體重過輕"         
        else:
            status = "正常範圍"

        return "您的BMI值是{}\n您的體重{}".format(bmi, status)
    except Exception as e:
        print("未知錯誤", e)

try:
    height = Decimal(input("請輸入身高(公分)"))
    weight = Decimal(input("請輸入體重(公斤)"))
    print(getBMI(height, weight))
except Exception as e:
    print("請輸入正確的身高、體重,須為數字")
victor12380 commented 2 months ago
try:
    high = float(input("請輸入身高(公分):"))
    weight = float(input("請輸入體重(公斤)"))

    if weight > 500 or weight < 10 :
        raise Exception("體重範圍錯誤")
    if high > 300 or high < 10 :
        raise Exception("身高範圍錯誤")

    print(f"身高{high}公分,體重{weight}公斤") 
    bmi  = weight/((high/100)**2)
    print( "您的BMI是{0:3.2f}".format(bmi))

    if bmi >=35:
        print("您是重度肥胖")
    elif bmi>=30:
        print("您是中度肥胖")
    elif bmi>=27:
        print("您是輕度度肥胖")
    elif bmi>=24:
        print("您的體重過重")    
    elif bmi<18.5:
        print("您的體重過輕")    
    else:
        print("您的體重正常")

except ValueError:
    print("輸入格式錯誤")
except Exception as e:
    print(f"錯誤訊息:{e}")
except Exception:
    print("不知名錯誤")
Liweii99 commented 2 months ago
#BMI計算公式: BMI=體重(Kg)/身高(m)**2
try:   
    is_add = input("您是否為成年人?(y,n)")
    if is_add == 'y':
        height = float(input("請輸入身高(公分):"))
        if height > 190 or height < 140:
            raise Exception("請接洽醫師")
        weight = float(input("請輸入體重(公斤)"))
        if weight>120 or weight<30:
            raise Exception("請接洽醫師") 
        print(f"身高{height}公分,體重{weight}公斤") 

        bmi  = weight/((height/100)**2)
        print(f"BMI是:{bmi:.1f}")
        if bmi >=35:
            print("您是重度肥胖")
        elif bmi>=30:
            print("您是中度肥胖")
        elif bmi>=27:
            print("您是輕度度肥胖")
        elif bmi>=24:
            print("您的體重過重")    
        elif bmi<18.5:
            print("您的體重過輕")
        else:
            print("您的體重正常")    
except ValueError:
    print("輸入格式有錯")
except Exception as e:
    print(f"異常訊息:{e}")

print("謝謝您的回應")
2021yuchen commented 2 months ago
try:
    # 輸入身高(公分)與體重(公斤)
    height_cm = int(input("請輸入身高(公分): "))
    weight_kg = int(input("請輸入體重(公斤): "))

    # 檢查輸入的數值是否為合理範圍
    if height_cm <= 0 or weight_kg <= 0:
        raise ValueError("身高和體重必須是正數")

    # 將身高轉換為公尺
    height_m = height_cm / 100

    # 計算 BMI 值
    bmi = weight_kg / (height_m ** 2)

    # 輸出 BMI 值
    print(f"您的BMI值為{bmi:.1f}")

    # 判斷 BMI 狀況
    if bmi < 18.5:
        print("您的體重過輕")
    elif 18.5 <= bmi < 24:
        print("正常範圍")
    elif 24 <= bmi < 27:
        print("您的體重過重")
    elif 27 <= bmi < 30:
        print("輕度肥胖")
    elif 30 <= bmi < 35:
        print("中度肥胖")
    else:
        print("重度肥胖")

except ValueError as e:
    print(f"輸入格式錯誤: {e}")

except Exception as e:
    print(f'錯誤訊息: {e}')

print("應用程式結束")
AnnaCheng58796907 commented 2 months ago
# 20240907 homework BMI計算

try:
    height_cm = float(input("請輸入身高(公分):"))
    weight_kg = float(input("請輸入體重(公斤):"))

    height_m = round(height_cm/100, 2)

    bmi_kg_m2 = round(weight_kg/(height_m**2), 2)

    if bmi_kg_m2 >= 35 :
        print(f"您的BMI值是{bmi_kg_m2}\n您的體重重度肥胖")
    elif bmi_kg_m2 >= 30 :
        print(f"您的BMI值是{bmi_kg_m2}\n您的體重中度肥胖")
    elif bmi_kg_m2 >= 27 :
        print(f"您的BMI值是{bmi_kg_m2}\n您的體重輕度肥胖") 
    elif bmi_kg_m2 >= 24 :
        print(f"您的BMI值是{bmi_kg_m2}\n您的體重過重")
    elif bmi_kg_m2 >= 18.5 :
        print(f"您的BMI值是{bmi_kg_m2}\n您的體重正常範圍")
    else :
        print(f"您的BMI值是{bmi_kg_m2}\n您的體重過輕") 
except Exception:
    print('輸入格式錯誤')

print("程式結束")
rogerchen0226 commented 2 months ago
#  homework BMI計算
try: 
    print("計算公式 BMI = 體重(公斤)/身高\u00B2(公尺\u00B2)")

    height = float(input("請輸入身高(公分):"))

    weight = float(input("請輸入體重(公斤)"))

    if height<=0 or weight<=0:
        raise ValueError("身高和體重必須都大於0")
    else:        
        print(f"您的身高 {height} 公分,體重 {weight} 公斤")

        bmi  = weight/((height/100)**2)

        print(f"您的BMI值是{bmi:.1f} ", end="")

        if bmi >=35:
            print("屬於重度肥胖")
        elif bmi>=30:
            print("屬於中度肥胖")
        elif bmi>=27:
            print("屬於輕度度肥胖")
        elif bmi>=24:
            print("屬於過重")
        elif bmi<18.5:
            print("屬於過輕")
        else:
            print("屬於正常範圍")

except ValueError as e:
    print(f"輸入有誤:{e}")

except Exception as e:
    print(f"發生異常:{e}")

print("程式處理結束")
Yiling-mari commented 2 months ago
#預防crash&設定變數
try: 
    height = int(input("請輸入身高(公分):"))
    weight = int(input("請輸入體重(公斤):"))
except:
    print("輸入錯誤,請重新輸入")   
else: #計算BMI
    height /=100
    BMI = round(weight/height**2, 1)
    print(f"您的BMI值是{BMI}")            
    if BMI < 18.5:          #體重分析
        print ("您的體重過輕")
    elif BMI <= 24:
        print ("您的體重在正常範圍")
    elif BMI <= 27:
        print ("您的體重過重")
    elif BMI <= 30:
        print ("您的體重輕度肥胖")
    elif BMI <= 35:
        print ("您的體重重度肥胖")
    else:
        print ("請趕快減重!")

print("應用程式結束")
Hank491 commented 2 months ago
kg=0  #清除變數
cm=0  #清除變數
try:    
    cm = int(input("請輸入身高(公分):"))
    if cm > 300:
            raise Exception("超過300公分")
except ValueError:
    print('輸入格式錯誤')
except Exception as e:
    print(f'輸入錯誤{cm}')

try:    
    kg = int(input("請輸入體重(公斤):"))
    if kg > 300:
          raise Exception("超過300公分")
except ValueError:
    print('輸入格式錯誤')
except Exception as e:
    print(f'輸入錯誤{kg}')

print(f'身高={cm},體重={kg}')
cm=(cm/100)*(cm/100)
BMI=kg/cm
print(f'BMI={BMI}')
if BMI >=35:
    print("重度肥胖:BMI≧35")
elif BMI >=30:
    print("中度肥胖:30≦BMI")
elif BMI >=27:
    print("輕度肥胖:27≦BMI")
elif BMI >=24:
    print("過重")
elif BMI >=18.5:
    print("正常範圍")
else:
    print("體重過輕")
print('程式結束')
U120471683 commented 2 months ago

try: height_cm = float(input("請輸入您的身高(公分): ")) weight_kg = float(input("請輸入您的體重(公斤): "))

將身高轉換為公尺
height_m = height_cm / 100
計算BMI值
bmi = weight_kg / (height_m ** 2)
判斷體重狀況
if bmi < 18.5:
    status = "體重過輕"
elif 18.5 <= bmi < 24:
    status = "體重正常"
elif 24 <= bmi < 27:
    status = "過重"
elif 27 <= bmi < 30:
    status = "輕度肥胖"
elif 30 <= bmi < 35:
    status = "中度肥胖"
else:
    status = "重度肥胖"
輸出結果
print(f"您的BMI值為: {bmi:.2f}")
print(f"您的體重狀況為: {status}")    
處理例外情況

except ValueError: print("輸入格式錯誤") except Exception as e: print(f"錯誤訊息:{e}") except Exception: print("不知名錯誤")

wukoyu commented 2 months ago
try:
    Height = int(input("請輸入身高(公分):"))
    Weight = int(input("請輸入體重(公斤):"))

    BMI = Weight / (Height*Height /10000)
    print("您的BMI值是:",BMI)
    if BMI < 18.5:
        print("您的體重過輕")
    elif BMI < 24:
        print("您的體重適中")
    elif BMI < 27:
        print("您的體重有點重")
    elif BMI < 30:
        print("您的體重輕度肥胖")
    elif BMI < 35:
        print("您的體重中度肥胖")
    else:
        print("您的體重重度肥胖")

except ValueError:
    print("輸入格式有錯誤")

print("應用程式結束")
wenchieh-hsiao commented 2 months ago
try:
    height_cm=float(input("請輸入身高(公分)"))
    if height_cm<=0:
        raise Exception("輸入身高格式錯誤")
    weight=float(input("請輸入體重(公斤)"))
    if weight<=0:
        raise Exception("輸入體重格式錯誤")
    height_m=float(height_cm)/100
    BMI=weight/height_m ** 2
    print(f"BMI值:{BMI}")
    if BMI >=35:
        print("重度肥胖")
    elif BMI >=30:
        print("中度肥胖")
    elif BMI >=27:
        print("輕度肥胖")
    elif BMI >=24:
        print("過重肥胖")              
    elif BMI >=18.5:
        print("健康體重")   
except ValueError:
    print("輸入格式錯誤")
except Exception as e:
    print(f"錯誤說明:{e}")
except Exception:
    print("不可預期的錯誤")
print("結束BMI運算程式")
JMsou commented 2 months ago
try:
    height = int(input('請輸入身高(公分):'))
    weight = int(input('請輸入體重(公斤):'))
    if height > 300 or height < 0:
        raise Exception('身高請輸入300公分以內')
    if weight < 0:
        raise Exception('請輸入正確體重')
    BMI = weight/((height/100)**2)
    print(f'你的BMI值是{BMI}')
    if BMI<18.5:
        print('您的體重過輕')
    elif BMI<24:
        print('您的體重正常')
    elif BMI<27:
        print('您的體重過重')
    elif BMI<30:
        print('您現在輕度肥胖')
    elif BMI<35:
        print('您現在中度肥胖')
    else:
        print('您現在重度肥胖')

except ValueError:
    print('請輸入純數字')
except Exception as e:
    print(e)
print('END')
William261831 commented 2 months ago
try:
    height = float(input("請輸入您的身高(公分)"))
    if height > 300 or height <= 0:
        raise Exception("身高須以公分為單位並在0~300範圍內")            
    weight = float(input("請輸入您的體重(公斤)"))
    if weight <= 0:
        raise Exception("體重須以公斤為單位並大於0")
    gender = input("請輸入您的性別(男/女):").strip()
    BMI = weight/(height/100) ** 2
    print(f'BMI = {BMI:.1f}')

    if gender == "男":
        if BMI < 20:
            category = "體重過輕"
        elif 20 <= BMI < 24:
            category = "正常範圍"
        elif 24 <= BMI < 27:
            category = "過重"
        elif 27 <= BMI < 30:
            category = "輕度肥胖"
        elif 30 <= BMI < 35:
            category = "中度肥胖"
        else:
            category = "重度肥胖"
    elif gender == "女":
        if BMI < 18:
            category = "體重過輕"
        elif 18 <= BMI < 22:
            category = "正常範圍"
        elif 22 <= BMI < 25:
            category = "過重"
        elif 25 <= BMI < 30:
            category = "輕度肥胖"
        elif 30 <= BMI < 35:
            category = "中度肥胖"
        else:
            category = "重度肥胖"
    else:
        category = "性別輸入錯誤"

    # 輸出體重類別
    if category != "性別輸入錯誤":
        print(f"範圍:{category}")
    else:
        print(category)
except ValueError:
    print("輸入格式有錯")
except Exception as e:
    print(f'錯誤訊息:{e}')
Changlizen commented 2 months ago
#https://depart.femh.org.tw/dietary/3OPD/BMI.htm
try:
        weight_kg = int(input("請輸入體重(公斤):"))
        height_cm = int(input("請輸入身高(公分):"))

        # 將身高從公分轉換為米
        height_m = height_cm / 100

        # 計算 BMI
        bmi = weight_kg/(height_m*height_m)

        print(f"您的 BMI 值為: {bmi:.2f}")

        # BMI 分類
        if bmi < 18.5:
            print("您的體重過輕,應適時補充所需能量")
        elif 18.5 <= bmi < 24:
            print("您的正常體重,請持續保持")
        elif 24 <= bmi < 27:
            print("您的體重過重,有空請多維持運動")
        elif 27 <= bmi < 30:
            print("您的體重屬於輕度肥胖,請多維持運動")
        elif 30 <= bmi < 35:
            print("您的體重屬於中度肥胖,請增加運動量")
        elif bmi > 35:
            print("您的體重屬於重度肥胖,請持續維持運動")         

except ValueError as e:
        print(f"輸入錯誤: {e}")

print("應用程式結束")