roberthsu2003 / __2024_01_28_sunday__

20 stars 2 forks source link

BMI,使用function #7

Open roberthsu2003 opened 8 months ago

roberthsu2003 commented 8 months ago
截圖 2024-02-18 下午3 20 57 截圖 2024-02-18 下午4 56 04

輸入:

請輸入身高(公分,120-250): 請輸入體重(公斤,30-250):

輸出:

您的BMI是:xxxx 您的體重:正常

自訂function

def getSuggestion(bmi:float) -> str:
    xxxxxx
    xxxxxx
   return xxxxx
Kenyon1119 commented 8 months ago
def getSuggestion(bmi:float) -> str:
    print(f'您的BMI值為:{bmi:.2f}')
    if bmi<18.5:
        print(f'範圍區間屬於過輕,多吃一點補充營養\n')
    elif bmi<24:
        print(f'範圍區間屬於正常\n')
    elif bmi<27:
        print(f'範圍區間屬於過重,注意飲食\n')
    elif bmi<30:
        print(f'範圍區間屬於輕度肥胖,要多運動\n')
    elif bmi<35:
        print(f'範圍區間屬於中度肥胖,該注意飲食外要多加運動\n')
    else:  
        print(f'範圍區間屬於重度肥胖,請與醫師與健身教練聯絡諮詢\n') 
import pyinputplus as pyip
while(True):
    print("----BMI計算程式開始----\n")
    height=0
    weight=0
    try:
        height:float=eval(input("請輸入您的身高 ( 120~250 公分)"))
    except:
        print('錯誤訊息!請輸入數字')
    else:
        if height>=120 and height<=250:
            print(f'您的身高為:{height:.2f}公分')
            try:
                weight:float=eval(input("請輸入您的體重 (  30~250 公斤)"))
            except:
                print('錯誤訊息!請輸入數字')
            else:
                if weight>=30 and weight<=250:
                    print(f'您的體重是:{weight:.2f}公斤')
                    bmi=weight/((height/100)**2)
                    getSuggestion(bmi)
                else:
                    print(f'您輸入{weight}公斤,超出輸入範圍30~250公斤,請重新輸入')
        else:
            print(f'您輸入{height}公分,超出輸入範圍120~250公分,請重新輸入')
    menu_value = pyip.inputMenu(["yes","no"],prompt="請問您還要繼續測量BMI嗎?\n",numbered=True)
    if menu_value == 'no':
        print("----BMI計算程式結束----\n")
        break
chiehyun2024 commented 8 months ago
#自訂的function
def getSuggestion(bmi:float) -> str:
    print(f'您的BMI值為:{bmi:.2f}')
    if bmi<18.5:
        print('您的體重:體重過輕')
    elif bmi<24:
        print('您的體重:正常')
    elif bmi<27:
        print('您的體重:過重')
    elif bmi<30:
        print('您的體重:輕度肥胖')
    elif bmi<35:
        print('您的體重:中度肥胖')
    else:
        print('您的體重:重度肥胖')

    return bmi 

try:
    height = eval(input('請輸入身高(公分,120-250):'))
    weight = eval(input('請輸入體重(公斤,30-250):'))
    if (height >= 120 and height<=250) and (weight >=30 and weight <=250) :
        bmi = weight/(height/100)**2
        getSuggestion(bmi)
    else :
        print('身高或體重超出範圍')
except:
    print('輸入格式有錯誤')
johnnycheng0924 commented 8 months ago
#BMI 自訂function
#try,expect
def getSuggestion(BMI:float)->str:
    print(f"你的BMI是{BMI:.2f}")
    if(BMI<18.5):
        print("你的體重過輕")
    elif(BMI>18.5 and BMI<24):
        print("你的體重正常")
    elif(BMI>24 and BMI<27):
        print("你的體重過重")
    elif(BMI>27 and BMI<30):
        print("你的體重輕度肥胖")
    elif(BMI>30 and BMI<35):
        print("你的體重中度肥胖")
    elif(BMI>35):
        print("你的體重重度肥胖")
    return BMI
try:
    height=int(input("請輸入身高(公分,120-250)"))
    print(height)
    if height>=120 and height<=250:
        print("在120-250之間")
    else:
        print("不在範圍內")
    weight=int(input("請輸入體重(公斤,30-250)"))
    print(weight)
    if weight>=30 and weight<=250:
        print("在120-250之間")
        BMI=weight/((height/100)**2)
        getSuggestion(BMI)
    else:
        print("不在範圍內")
except:
    print("輸出錯誤訊息")     
angelaleepython commented 8 months ago
#BMI
#try,expect
def getSuggestion(BMI:float)->str:
    print(f"你的BMI是{BMI:.2f}")
    if(BMI<18.5):
        print("你的體重過輕")
    elif(BMI>18.5 and BMI<24):
        print("你的體重正常")
    elif(BMI>24 and BMI<27):
        print("你的體重過重")
    elif(BMI>27 and BMI<30):
        print("你的體重輕度肥胖")
    elif(BMI>30 and BMI<35):
        print("你的體重中度肥胖")
    elif(BMI>35):
        print("你的體重重度肥胖")
    return BMI
try:
    height=int(input("請輸入身高(公分,120-250)"))
    print(height)
    if height>=120 and height<=250:
        print("在120-250之間")
    else:
        print("不在範圍內")
    weight=int(input("請輸入體重(公斤,30-250)"))
    print(weight)
    if weight>=30 and weight<=250:
        print("在120-250之間")
        BMI=weight/((height/100)**2)
        getSuggestion(BMI)
    else:
        print("不在範圍內")
except:
    print("輸出錯誤訊息")     
tata1330 commented 8 months ago
import pyinputplus as pyip

def getSuggestion(bmi:float) -> str:
    if bmi<18.5:
        print('您的體重:體重過輕')
    elif bmi<24:
        print('您的體重:正常')
    elif bmi<27:
        print('您的體重:過重')
    elif bmi<30:
        print('您的體重:輕度肥胖')
    elif bmi<35:
        print('您的體重:中度肥胖')
    else:
        print('您的體重:重度肥胖')

def main():
    while(True): 
        try:
            height = int(input('請輸入身高(120-250公分):'))

            if height >= 120:    
                if height <= 250:
                    print(f'身高:{height:.0f}公分')
                    weight = int(input('請輸入體重(30-250公斤):'))

                    if weight >= 30:
                        if weight<=250:
                            print(f'體重:{weight:.0f}公斤')
                            bmi = weight/(height/100)**2
                            print(f'您的BMI是:{bmi:.2f}')
                            getSuggestion(bmi)
                        else:
                            print('體重大於250公斤,不在範圍內,請重新輸入範圍內的體重')
                            continue
                    else:
                        print('體重小於30公斤,不在範圍內,請重新輸入範圍內的體重')
                        continue
                else:
                    print('身高大於250公分,不在範圍內,請重新輸入範圍內的身高')
                    continue
            else:
                print('身高小於120公分,不在範圍內,請重新輸入範圍內的身高')
                continue
        except:
            print('輸入格式有錯誤,請重新輸入')
            continue

        menu_value = pyip.inputMenu(["yes","no"], prompt = "你還要繼續嗎?\n", numbered = True)
        if menu_value == "no":
            break
    print("程式結束")

if __name__ == '__main__':
    main()
StanleyStanley1234 commented 8 months ago
def formula(bmi):
  bmi=weight/(height/100)**2
  if(bmi<18.5):
   message="Underweight"
  elif(18.5<=bmi<24):
   message="Your weight is in the normal range."
  elif(24<=bmi<27):
   message="Mild obesity"
  elif(27<=bmi<30):
   message="Moderately obese"
  else:
   message="Overweight"
  return message
while(True):
 try:
  height=float(input("Please enter your height (cm)"))
  weight=float(input("Please enter your weight (kg)"))
  if(120<=height<=250)and(30<=weight<=250):
    bmi=weight/(height/100)**2
    print(f"Your BMI is {formula(bmi)}")
    print(f"Your weight is {weight}")
  else:
    print("The height or weight is out of the range")
    print("height(120-250),weight(30-250)")
 except:
  print("The number that you enter is in the wrong format")

 value=eval(input("Do you want to continue this BMI test ? 1.Yes 2.No"))
 if(value==1):
  continue
 elif(value==2):
  break
 else:
  print("There is no such option.")
BettyyWeng commented 7 months ago
import pyinputplus as pyip

def getSuggestion(bmi:float) -> str:
    print(f'您的BMI值為:{bmi:.2f}')
    if bmi<18.5:
        message = '您的體重:體重過輕'
    elif bmi<24:
        message = '您的體重:正常'
    elif bmi<27:
        message = '您的體重:過重'
    elif bmi<30:
        message = '您的體重:輕度肥胖'
    elif bmi<35:
        message = '您的體重:中度肥胖'
    else:
        message = '您的體重:重度肥胖'

    return message

while(True): 
    print("_____BMI計算程式開始_____")
    try:
        height = eval(input('請輸入身高(公分,120-250):'))
        weight = eval(input('請輸入體重(公斤,30-250):'))
        if (height >= 120 and height<=250) and (weight >=30 and weight <=250) :
            bmi = weight/(height/100)**2
            print(getSuggestion(bmi))
        else :
            print('身高或體重超出範圍')
    except:
        print('輸入格式有錯誤')
    menu_value = pyip.inputMenu(["yes","no"],prompt = "請問您還要繼續測量BMI嗎?\n",numbered=True)
    if menu_value == "no":
        print("BMI計算程式結束")
        break
BettyyWeng commented 7 months ago
import pyinputplus as pyip

def getSuggestion(bmi:float) -> str:
    print(f'您的BMI值為:{bmi:.2f}')
    if bmi<18.5:
        message = '您的體重:體重過輕'
    elif bmi<24:
        message = '您的體重:正常'
    elif bmi<27:
        message = '您的體重:過重'
    elif bmi<30:
        message = '您的體重:輕度肥胖'
    elif bmi<35:
        message = '您的體重:中度肥胖'
    else:
        message = '您的體重:重度肥胖'

    return message

while(True): 
    print("_____BMI計算程式開始_____")
    try:
        height = eval(input('請輸入身高(公分,120-250):'))
        weight = eval(input('請輸入體重(公斤,30-250):'))
        if (height >= 120 and height<=250) and (weight >=30 and weight <=250) :
            bmi = weight/(height/100)**2
            print(getSuggestion(bmi))
        else :
            print('身高或體重超出範圍')
    except:
        print('輸入格式有錯誤')
    menu_value = pyip.inputMenu(["yes","no"],prompt = "請問您還要繼續測量BMI嗎?\n",numbered=True)
    if menu_value == "no":
        print("BMI計算程式結束")
        break