Open roberthsu2003 opened 1 week ago
# 20241116 homework BMI計算
def get_status(bmi:float)->str:
if bmi >= 35 :
bmi_str = '重度肥胖'
elif bmi >= 30 :
bmi_str = '中度肥胖'
elif bmi >= 27 :
bmi_str = '輕度肥胖'
elif bmi >= 24 :
bmi_str = '過重'
elif bmi >= 18.5 :
bmi_str = '正常範圍'
else :
bmi_str = '過輕'
return bmi_str
def BMI_math(height_cm:float, weight_kg:float):
height_m = round(height_cm/100, 2)
bmi_kg_m2 = round(weight_kg/(height_m**2), 2)
bmi_str = get_status(bmi_kg_m2)
return bmi_kg_m2, bmi_str
while(True):
try:
height_cm = float(input("請輸入身高(公分):"))
weight_kg = float(input("請輸入體重(公斤):"))
bmi_value, bmi_str = BMI_math(height_cm, weight_kg)
break
except Exception:
print('輸入格式錯誤,請重新輸入!')
print(f"您的BMI值是{bmi_value}\n您的體重{bmi_str}")
print("程式結束")
def get_status(bmi:float)->str:
if bmi >= 35 or bmi >= 24 or bmi < 18.5:
print("異常範圍")
if bmi >= 35 :
print("重度肥胖")
elif bmi >= 30 :
print("中度肥胖")
elif bmi >= 27 :
print("中度肥胖")
elif bmi >= 24 :
print("過重")
elif bmi < 18.5:
print("體重過輕")
elif bmi >=18.5 and bmi < 24:
print("正常範圍")
while(True):
try:
height = int(input("請輸入身高(公分):"))
weight = int(input("請輸入體重(公斤):"))
except Exception:
print('輸入格式錯誤 ,請重新輸入')
continue
else:
bmi = round(weight /((height / 100) ** 2),2)
get_status(bmi)
print(f"您的BMI值是{bmi}\n您的體重{weight}")
break
print("應用程式結束")
def get_status(BMI:float)->str:
print(f"BMI值:{BMI}")
if BMI >=35:
BMI_str="重度肥胖"
elif BMI >=30:
BMI_str="中度肥胖"
elif BMI >=27:
BMI_str="輕度肥胖"
elif BMI >=24:
BMI_str="過重"
elif BMI >=18.5:
BMI_str="健康體重"
return BMI_str
while True:
try:
height = float(input('請輸入身高(cm):'))
weight= float(input('請輸入體重(kg):'))
if height<0 or weight<0:
raise Exception
except ValueError:
print("使用者輸入錯誤,請重新輸入")
except Exception:
print("使用者輸入負數,身高體重不能為負數,請從新輸入")
continue
else:
BMI=weight/((height/100)**2)
print(f"你的BMI是:{get_status(BMI)}")
print(f"你的體重是:{weight}")
while (EndProgram := input("重新輸入請按1,離開程式請按0: ")) not in ('1', '0'):
print("無效輸入,請輸入1或0。")
if EndProgram =='1':
continue
else:
break
print("結束BMI運算程式")
def get_status(bmi:float)->str:
outStatus = ""
if bmi >= 35 :
outStatus = '重度肥胖'
elif bmi >= 30 :
outStatus = '中度肥胖'
elif bmi >= 27 :
outStatus = '輕度肥胖'
elif bmi >= 24 :
outStatus = '體重過重'
elif bmi >= 18.5 :
outStatus = '正常範圍'
else :
outStatus = '體重過輕'
return outStatus
def calculate_bmi(inHeigh:float,inWeight:float)->float:
outBMI = round(inWeight / ((inHeigh/100)**2), 2)
return outBMI
while(True):
try:
inputheigh=input('請輸入身高(cm):')
heigh = float(inputheigh)
break
except Exception:
print(inputheigh+",身高格式錯誤,請輸入數字!")
continue
while(True):
try:
inputweight=input('請輸入體重(kg)')
weight = float(inputweight)
break
except Exception:
print(inputweight+",體重格式錯誤,請輸入數字!")
continue
BMI = calculate_bmi(heigh,weight)
status = get_status(BMI)
print(f'您的BMI是:{BMI}')
print(f'您的體重是:{weight}')
print(f'您的BMI指數:{status}')
def calculate_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 "異常範圍,重度肥胖"
while (True):
try:
height = int(input("請輸入身高(cm): "))
weight = int(input("請輸入體重(kg): "))
BMI = calculate_bmi(height,weight)
Health = get_status(BMI)
print(f"您的身高、體重為{height}公分、{weight}公斤。BMI為 {int(BMI)},{Health}")
break
except Exception:
print("發生錯誤!,請重新輸入正確數值")
def get_status(bmi:float) -> str:
print(f'BMI={bmi}')
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
while True:
try:
height = float(input('請輸入身高(cm):'))
weight = float(input('請輸入體重(kg):'))
except ValueError:
print('請輸入數字')
except Exception:
print('發生錯誤')
else:
bmi = weight / (height/100)**2
status = get_status(bmi)
print(f'體重狀態{status}')
break
print('程式結束')
#Homework
def get_status(bmi:float)->str:
if BMI >=35:
return "重度肥胖"
elif BMI >=30:
return "中度肥胖"
elif BMI >=27:
return "輕度肥胖"
elif BMI >=24:
return "過重肥胖"
elif BMI >=18.5:
return "健康體重"
else:
return "體重過輕"
while True:
try:
height_cm=float(input("請輸入身高(公分)"))
if height_cm<=0 or height_cm > 300:
raise Exception("輸入身高格式錯誤")
weight=float(input("請輸入體重(公斤)"))
if weight<=0:
raise Exception("輸入體重格式錯誤")
except ValueError:
print("輸入格式錯誤")
continue
except Exception as e:
print(f"錯誤說明:{e}")
continue
except Exception:
print("不可預期的錯誤")
continue
else:
height_m=float(height_cm)/100
BMI=weight/height_m ** 2
print(f"BMI值:{BMI}")
receive_status = get_status(BMI)
print(receive_status)
again = input("請問還需要再繼續計算BMI值嗎?(y/n)")
if again == "n" or again =="N":
break
print("結束BMI運算程式")
# 20241116 homework
'''要檢查使用者輸入的格式,如果輸入錯誤,要求使用者重新輸入
-請將計算體重狀態,定義一個function'''
def get_status(bmi_kg_m2:float)->str:
if bmi_kg_m2 >= 35 :
bmi = '重度肥胖'
elif bmi_kg_m2 >= 30 :
bmi = '中度肥胖'
elif bmi_kg_m2 >= 27 :
bmi = '輕度肥胖'
elif bmi_kg_m2 >= 24 :
bmi = '過重'
elif bmi_kg_m2 >= 18.5 :
bmi = '正常範圍'
else :
bmi = '過輕'
return bmi
def cal_bmi(height_cm,weight_kg):
return round(weight_kg/((height_cm/100)**2), 2)
while (True):
try:
height_cm = float(input("請輸入身高(公分):"))
weight_kg = float(input("請輸入體重(公斤):"))
except Exception:
print('輸入格式錯誤,請重新輸入')
continue
else:
break
your_bmi=cal_bmi(height_cm,weight_kg)
print(f'Your BMI is {your_bmi}.')
your_bmi_comment=get_status(your_bmi)
print(f'根據計算您的BMI,結果是 "{your_bmi_comment}"')
print("程式結束")
def bmi_status(bmi:float)->str:
if bmi < 18.5:
status ="過輕"
elif bmi >= 18.5 and bmi < 24:
status = "正常範圍"
elif bmi >= 24 and bmi < 27:
status = "過重"
elif bmi >= 27 and bmi < 30:
status = "輕度肥胖"
elif bmi >= 30 and bmi < 35:
status = "中度肥胖"
else:
status ="重度肥胖"
return status
def new_func():
while(True):
try:
height = int(input("請輸入身高(公分)"))
weight = int(input("請輸入體重(公斤)"))
if height <= 0 or weight <= 0:
print("身高和體重必須是正數!")
continue
except Exception:
print("輸入錯誤,請輸入有效的數字")
continue
else:
bmi = weight /(height/100)**2
status = bmi_status(bmi)
print(f"您輸入的體重:{weight}(Kg)及身高:{height}(cm),\n計算後取得的BMI值約為{bmi:.2f},\n您的BMI被歸類為{status}。")
break
#初始計算次數
counter = 0
while(True):
new_func()
#每計算過一次加一
counter +=1
print(f"=======以上為您第{counter}次輸入後計算的BMI資料=======")
responed = input("請問您還要再計算其他BMI嗎?(y,n) ").lower().strip() #自動將人員輸入空格刪除及轉換成小寫
if responed != "y":
break
print(f"程式結束。總共計算了 {counter} 次 BMI。")
def input_data():
while True:
try:
cm = int(input("請輸入身高(公分):"))
if cm > 300:
raise Exception("超過300公分")
break
except ValueError:
print('輸入格式錯誤,請重新輸入')
continue
except Exception as e:
print(f'限300內,您的值{cm}請重新輸入')
continue
while True:
try:
kg = int(input("請輸入體重(公斤):"))
if kg > 300:
raise Exception("超過300公斤")
break
except ValueError:
print('輸入格式錯誤,請重新輸入')
continue
except Exception as e:
print(f'限300內,您的值{kg}請重新輸入')
continue
return cm,kg
def get_status(bmi:float)->str:
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("體重過輕")
while True:
kg=0
cm=0
cm,kg = input_data()
print(f'身高={cm},體重={kg}')
cm=(cm/100)*(cm/100)
BMI=round((kg/cm),2)
print(f'BMI={BMI}')
get_status(BMI)
play_again = input("還要繼續嗎?(y,n)")
if play_again == "n":
break
print('程式結束')
def get_status(bmi: float) -> str: if 18.5 <= bmi < 24: bmi_status = "不瘦不胖" elif 24 <= bmi < 27: bmi_status = "有點過重囉" elif 27 <= bmi < 30: bmi_status = "輕微胖胖" elif 30 <= bmi < 35: bmi_status = "中度胖胖" elif bmi >= 35: bmi_status = "過重胖胖" else: bmi_status = "瘦皮猴" return bmi_status
while(True): try: height = float(input("請輸入您的身高(公分):")) weight = float(input("請輸入您的體重(公斤):")) break except ValueError: print("請輸入數字")
bmi = weight / (height / 100) ** 2 bmi_status = get_status(bmi)
print(f'您的身高為:{height} 公分') print(f'您的體重為:{weight} 公斤') print(f'BMI結果:{bmi_status}') print(f"您的BMI值為:{bmi:.2f}") print("結束了~該吃吃喝喝了")
def get_status(bmi: float) -> str: if 18.5 <= bmi < 24: bmi_status = "不瘦不胖" elif 24 <= bmi < 27: bmi_status = "有點過重囉" elif 27 <= bmi < 30: bmi_status = "輕微胖胖" elif 30 <= bmi < 35: bmi_status = "中度胖胖" elif bmi >= 35: bmi_status = "過重胖胖" else: bmi_status = "瘦皮猴" return bmi_status
while(True): try: height = float(input("請輸入您的身高(公分):")) weight = float(input("請輸入您的體重(公斤):")) break except ValueError: print("請輸入數字")
bmi = weight / (height / 100) ** 2 bmi_status = get_status(bmi)
print(f'您的身高為:{height} 公分') print(f'您的體重為:{weight} 公斤') print(f'BMI結果:{bmi_status}') print(f"您的BMI值為:{bmi:.2f}") print("結束了~該吃吃喝喝了") [image: image] https://private-user-images.githubusercontent.com/187077172/387699820-219afc8f-dd35-4eb6-b35d-2f4bc7986329.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3MzIwMjc0MzcsIm5iZiI6MTczMjAyNzEzNywicGF0aCI6Ii8xODcwNzcxNzIvMzg3Njk5ODIwLTIxOWFmYzhmLWRkMzUtNGViNi1iMzVkLTJmNGJjNzk4NjMyOS5wbmc_WC1BbXotQWxnb3JpdGhtPUFXUzQtSE1BQy1TSEEyNTYmWC1BbXotQ3JlZGVudGlhbD1BS0lBVkNPRFlMU0E1M1BRSzRaQSUyRjIwMjQxMTE5JTJGdXMtZWFzdC0xJTJGczMlMkZhd3M0X3JlcXVlc3QmWC1BbXotRGF0ZT0yMDI0MTExOVQxNDM4NTdaJlgtQW16LUV4cGlyZXM9MzAwJlgtQW16LVNpZ25hdHVyZT05MWMyM2YxYmQzZGJhZWQ2MTc2ZjlkZDAyNzI4MmUwYWM3YTE4OGVmMDk5ZjIyNDFjZDcxOTExY2ZjMWRmYThjJlgtQW16LVNpZ25lZEhlYWRlcnM9aG9zdCJ9.xMYQrEawcLBxSpdrS5QS0AOg0uguJ0ie5AwhRYZRZr0
黃力威 @.***> 於 2024年11月19日 週二 下午8:45寫道:
def input_data(): while True: try: cm = int(input("請輸入身高(公分):")) if cm > 300: raise Exception("超過300公分") break except ValueError: print('輸入格式錯誤,請重新輸入') continue except Exception as e: print(f'限300內,您的值{cm}請重新輸入') continue while True: try: kg = int(input("請輸入體重(公斤):")) if kg > 300: raise Exception("超過300公斤") break except ValueError: print('輸入格式錯誤,請重新輸入') continue except Exception as e: print(f'限300內,您的值{kg}請重新輸入') continue return cm,kg
def get_status(bmi:float)->str: 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("體重過輕")
while True: kg=0 cm=0 cm,kg = input_data()
print(f'身高={cm},體重={kg}') cm=(cm/100)*(cm/100) BMI=round((kg/cm),2) print(f'BMI={BMI}') get_status(BMI) play_again = input("還要繼續嗎?(y,n)") if play_again == "n": break
print('程式結束')
image.png (view on web) https://github.com/user-attachments/assets/7396194c-8dba-432f-a7fa-85a067446d90
— Reply to this email directly, view it on GitHub https://github.com/roberthsu2003/__2024_11_02_chihlee_api__/issues/2#issuecomment-2485618941, or unsubscribe https://github.com/notifications/unsubscribe-auth/BMTJENERLUACAG4ILEXISQL2BMXHXAVCNFSM6AAAAABR4SXBF6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDIOBVGYYTQOJUGE . You are receiving this because you are subscribed to this thread.Message ID: @.*** com>
def get_status():
while True:
try:
身高 = float(input("請輸入身高(cm): "))
體重 = float(input("請輸入體重(kg): "))
except ValueError:
print("輸入錯誤,請重新輸入!")
else:
break
print(f"身高 = {身高} cm")
print(f"體重 = {體重} kg")
BMI = 體重 / ((身高 / 100) ** 2)
print(f"BMI = {BMI:.2f}")
if BMI >= 35:
print("肥胖程度: 重度肥胖")
elif BMI >= 30:
print("肥胖程度: 中度肥胖")
elif BMI >= 27:
print("肥胖程度: 輕度肥胖")
elif BMI >= 24:
print("肥胖程度: 過重")
elif BMI >= 18.5:
print("肥胖程度: 正常")
else:
print("肥胖程度: 過輕")
# 主程式
while True:
get_status()
再試一次 = input("是否要重新計算?(y/n): ")
if 再試一次 != 'y':
break
print("計算結束")
、、、 def get_status(BMI: float) -> str: if 18 <= BMI < 24: status = "您的BMI值過輕" elif 24 <= BMI < 27: status = "您的BMI值正常" elif 27 <= BMI < 30: status = "您是小胖豬" elif 30 <= BMI < 35: status = "您是大胖豬" else: status = "別懷疑,您是大肥豬" return status
while(True): try: wight = int(input("請輸入體重")) high = int(input("請輸入身高")) if high <= 0 or wight <= 0: print("身高和體重必須是正數") break except ValueError: print("輸入錯誤,請重新輸入")
high2 = high / 100 BMI = wight / (high2 ** 2)
print(f'您的身高是:{high}公分') print(f'您的體重是:{wight}公斤') # 這裡應該是公斤 print(f'您的BMI是:{BMI:.2f}') status = get_status(BMI) print(status)