#!/usr/bin/python # -*- coding: utf-8 -*- #小明身高1.75,體重80.5kg。請根據BMI公式(體重除以身高的平方) #幫小明計算他的BMI指數,並根據BMI指數: #低於18.5:過輕 # 18.5-25:正常 # 25-28:過重 # 28-32:肥胖 # 高於32:嚴重肥胖
import logging
def lgq(height,weight): bmi =weight/height bmi = bmi / height
return bmi
def shengao(height): height = input('歡迎使用我司產品,請輸入身高m:\n') if height.strip()=='': print('請重新輸入您的身高') return shengao(height) height = float(height) if height <= 0: print('身高數據要大於0哦') return shengao(height) elif height>3: print('您輸入的數據不能大於3哦') return shengao(height)
else: return height def tizhong(weight): weight = input('請輸入體重kg:\n') if weight.strip()=='': print('請重新輸入您的體重') return tizhong(weight) weight = float(weight) if weight <= 0: print('體重數據要大於0') return tizhong(weight) else: return weight
print('•低於18.5:過輕\n •18.5-25:正常\n •25-28:過重\n •28-32:肥胖\n •高於32:嚴重肥胖\n') bmi =0 height = 0 weight = 0 try:
height = shengao(height)
weight = tizhong(weight)
except ValueError as e: print('您輸入的數據有誤,請輸入大於0的數字')
try: bmi = lgq(height,weight) except ZeroDivisionError as e: print('身高不能輸入數據為0') if bmi>18.5 and bmi<=25: print('您的bmi指數為%.2f' % bmi,'您的體重正常') elif bmi>25 and bmi<=28: print('您的bmi指數為%.2f' % bmi,'您的體重過重') elif bmi>28 and bmi<=32: print('您的bmi指數為%.2f' % bmi,'您的體重肥胖') elif bmi>32: print('您的bmi指數為%.2f' % bmi,'你的體重嚴重肥胖') else: print('您的bmi指數為%.2f' % bmi,'您的體重太瘦了')