roberthsu2003 / __2024_04_17_mon_wed__

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

計算BMI,檢查輸入格式是否有錯誤 #6

Open roberthsu2003 opened 2 months ago

roberthsu2003 commented 2 months ago

輸入:

輸出:

錯誤:

截圖 2024-05-01 晚上9 28 27
chesterXalan commented 2 months ago
try:
    name = input('請輸入姓名:')
    height = float(input('請輸入身高(cm):'))
    weight = float(input('請輸入體重(kg):'))

    bmi = weight / ((height / 100) ** 2)
    print(f'{name}, 您的BMI: {round(bmi, ndigits=2)}')

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

image

Tina54321 commented 2 months ago
try:
    name:str = input("請輸入姓名:")
    height:float = float(input("請輸入身高(cm):")) * 0.01
    weight:float = float(input("請輸入體重(kg):"))
    BMI = round(weight / (height ** 2), ndigits=2)
    print(f"{name},您的BMI:{BMI},BMI正常範圍為18.5~24")
except Exception as error:
    print(f"格式錯誤:{error}")

image

MurrayChuang commented 2 months ago
try:
    name = input("請輸入姓名")
    height = eval(input("請輸入身高(cm)"))
    weight = eval(input("請輸入體重(kg)"))

    print(f"{name},您的BMI:{weight / (height / 100) ** 2:.2f}")
except Exception as e:
    print(f"格式錯誤:{e}")
image
Tony840705 commented 2 months ago
try:
    Name = input('請輸入姓名:')
    Height = float(input('請輸入身高(cm):'))
    Weight = float(input('請輸入體重(kg):'))
    BMI = Weight / (Height/100) ** 2

    print(f'{Name},您的BMI為:{round(BMI,ndigits=2)}')

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

BMI計算

victor1629 commented 2 months ago
try:
    name:str=str(input("請輸入姓名:"))
    height:float=float(input("請輸入身高(cm):"))
    weight:float=float(input("請輸入體重(kg):"))
    BMI =weight/(height/100)**2
    print(f"{name},您的BMI{BMI:.2f}")
except Exception as a :
    print(f'格式錯誤{a}')

螢幕擷取畫面 2024-05-02 112217 螢幕擷取畫面 2024-05-02 111559

chiayuben commented 2 months ago
try:
    name:str=input('請輸入名字')
    weight:str|float=eval(input('請輸入重量(Kg)'))
    high:str|float=eval(input('請輸入身高(公分)'))

    BMI:str|float=weight/(high/100)**2
    print(f'你的名字是{name},體重為{weight},身高為{high},經計算後BMI為{round(BMI,ndigits=1)}')
    if BMI>=18.5:
        if BMI>=24:
            print('你的BMI偏高,可能要減肥了喔')
        else:
            print('BMI很標準繼續保持')
    else:
        if BMI <=18.5:
            print('你的BMI值偏低,記得多吃點喔')
except Exception as e :
    print(f'發生錯誤{e}請重新輸入')  

2024-05-02_121853 2024-05-02_122033

Neillo0514 commented 2 months ago

try: name:str = str(input('請輸入姓名:')) height:float = float(input('請輸入身高(cm):')) weight:float = float(input('請輸入體重(kg):')) bmi = weight / ((height / 100) ** 2) print(f'{name}, 您的BMI: {round(bmi, ndigits=2)}') except Exception as e: print(f'格式錯誤: {e}') image image

chihweihan commented 2 months ago
try:
    name:str = input("請輸入姓名:")
    height:int|float = eval(input("請輸入身高(cm)"))
    weight:int|float = eval(input("請輸入體重(kg)"))

    BMI:str = weight / (height / 100) ** 2 

    print(f"{name},您的身高:{height} cm,體重:{weight} kg。\n經計算過後,您的BMI:{round(BMI, ndigits=2)}")
    if BMI > 18.5:
        if BMI >= 24:
            print("要小心囉,「體重過重」了!")
        else:
            print("恭喜!「健康體重」,要繼續保持!")
    else:
        print("「體重過輕」,需要多運動,均衡飲食,以增加體能,維持健康!")
except Exception as e:
    print(f'輸入內容有誤:{e},請重新輸入。')

2

1 想請問要如何知道自己是哪裡輸入錯誤以及輸入的內容?

PercJK commented 2 months ago

螢幕擷取畫面 2024-05-04 104351 螢幕擷取畫面 2024-05-04 104643

try:
    name = input('請輸入姓名:')
    height = float(input('請輸入身高(cm):'))
    weight = float(input('請輸入體重(kg):'))

    bmi = weight / ((height / 100) ** 2)
    print(f'{name}, 您的BMI: {round(bmi, ndigits=2)}')

except Exception as e:
    print(f'格式錯誤: {e}')
vinceyeh commented 2 months ago
try:
    Name = input('請輸入姓名:')
    Height = float(input('請輸入身高(cm):'))
    Weight = float(input('請輸入體重(kg):'))
    BMI = Weight / (Height/100) ** 2

    print(f'{Name},您的BMI為:{round(BMI,ndigits=2)}')

except Exception as e:
    print(f'格式錯誤:{e}')
stoneli1117 commented 2 months ago
try:
    name:str =input('請輸入名字:')
    height = float(input('請輸入身高:'))
    weight = float(input('請輸入體重'))
    BMI = round(weight/((height/100)**2),2)
    print(f'{name}的BMI指數為{BMI}')
except Exception as e:
    print(f'格是錯誤:{e}')

image image

YEZZHUANLE commented 2 months ago

try: name = str(input("請輸入姓名")) height:int|float = int(input("請輸入身高(cm)")) weight:int|float = int(input("請輸入體重(kg)")) bmi = weight / ((height/100) **2) print(f"{name},你的BMI:{round(bmi,ndigits=1)}") except Exception as e: print(f"格式錯誤:{e}") 螢幕擷取畫面 2024-05-05 205943 螢幕擷取畫面 2024-05-05 210009

drama554 commented 2 months ago
try:
    name:str = input('請輸入姓名:')
    height:float = float(input('請輸入身高(cm):'))
    weight:float  = float(input('請輸入體重(kg):'))
    BMI = weight / ((height / 100) ** 2)
    print(f"{name},你的BMI:{BMI:.2f}")
except Exception as a:
    print(f"格式錯誤:{a}")

螢幕擷取畫面 2024-05-05 212420

g02170017 commented 2 months ago
try:
    name = input("請輸入您的姓名")
    height = float(input("請輸入您的身高:cm"))
    weight = float(input("請輸入您的體重:kg"))
    bmi = weight / ((height / 100) * 2)
    print(f"{name}您的bmi:{bmi:.2f}")
except Exception as e:
    print(f"發生錯誤{e}")

image

henry21209 commented 2 months ago
try:
    name:str = input("請輸入姓名:")
    height:float = float(input("請輸入身高:"))
    weight:float = float(input("請輸入體重:"))

    print(f"{name},您的BMI:{round(weight/(height/100)**2,ndigits=2)}") 
except Exception as e:
    print(f"格式錯誤{e}")

image image

chchfhah05 commented 2 months ago
try:
    name = input("輸入姓名")
    h = float(input("請輸入您的身高(cm)"))/100
    w = float(input("請輸入您的體重(kg)"))
    bmi =round(w/(h*h))
    print(bmi)
    if bmi <= 18.5:
        print("你太輕囉")
    if bmi >= 18.5 and bmi <= 25:
        print("你的體重正常")
    if bmi >= 25:
        print("你好肥")

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

螢幕擷取畫面 2024-05-06 191226

JamesHungWJ commented 2 months ago
try:
    name=input("請輸入您的姓名:")
    hieght=eval(input("請輸入您的身高(cm)"))
    wieght=eval(input("請輸入您的體重"))
    bmi=round(wieght/((hieght/100)**2),ndigits=2)
    print(f"{name},您的身高為{hieght},體重為{wieght},經計算BMI為{bmi}")
except Exception as e:
    print(f"發生錯誤{e}")

image

KIOVER998 commented 2 months ago
try:
    name = input("請輸入姓名")
    height = eval(input("請輸入身高(cm)"))
    weight = eval(input("請輸入體重(kg)"))

    print(f"{name},您的BMI:{weight / (height / 100) ** 2:.2f}")

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

螢幕擷取畫面 2024-05-08 150825

Vress0 commented 2 months ago
try:
    name = input('請輸入姓名:')
    height = float(input('請輸入身高(120~230)(cm):')) 
    if height<120 or height>230:
        raise Exception("身高必需大於120同時小於230")    
    weight = float(input('請輸入體重(40~170)(kg):'))
    if weight<40 or weight>170:
        raise Exception("體重必需大於40同時小於170")
    bmi = weight / (height / 100) ** 2
    print(f'{name}, 您的BMI: {round(bmi,ndigits=2)}')
    #您的體重:正常
except Exception as e:
    print(f'錯誤: {e}')

螢幕擷取畫面 2024-05-08 211407

ccanna commented 2 months ago
try:

    name = input("請輸入姓名:")
    height:int = int(input("請輸入身高(cm):"))
    weight:int = int(input("請輸入體重(kg)"))

    bmi = round(weight / (height/100)**2, ndigits=2)

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

    if bmi >=18.5 and bmi <= 24:
        print("恭喜!標準體態!")

    if bmi >24:
        print("建議每周333運動!")

    if bmi <18.5:
        print  ("建議多補充營養食物!")

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

image image