設有:
身高(釐米)- 100=標準體重(千克);如果一個人的體重與其標準體重偏差在正負 5%之間,顯示“體重正常”,否則顯示“體重超標”或者“體重不達標”。編寫程序,能處理用戶輸入的異常,並使用自定義的異常類來處理身高小於30cm,大於250cm的情況。
class HeightException(Exception):
pass
try:
height = int(input("請輸入您的身高(cm):"))
if height < 30 or height > 250:
raise HeightException
weight = int(input("請輸入您的體重(kg):"))
weights = height - 100
if weight > weights and weight - weights < 0.05*weights:
print("體重達標")
elif weight < weights and weights - weight < 0.05*weights:
print("體重達標")
else:
print("體重不達標")
except HeightException:
print('您輸入的身高有誤')