Today, the teacher specifically talked about exception handling, so add try except statement to this question
First of all, we must find out the common exceptions in Python. Take this question as an example. If the height and weight entered by the user are illegal, the user will be prompted
NameError
Exception throw statement usage
try:<Statement> #Run other code
except <Exception name>:
< Statement> #Throw the NameError exception
else:<Statement> #If there is no exception, execute here
The following is the BMI assessment, which is based on the BMI value to determine the international and domestic standards
# coding:utf-8
# Zhou Peng
import math
while(True):try:print('BMI value is weight (KG)/height(M)squared')
kg=eval(input('Please enter your weight(KG):'))
height=eval(input('Please enter your height (M)'))
BMI=float(kg/math.pow(height,2))
inter=''
domestic=''
# International Computing Standard
if BMI<=18.5:
inter='Thin'
domestic='Thin'
elif BMI<24:
inter='normal'
domestic='normal'
elif BMI<25:
inter='normal'
domestic='Overweight'
elif BMI<28:
inter='Overweight'
domestic='Overweight'
elif BMI<30:
inter='Overweight'
domestic='obesity'else:
inter='obesity'
domestic='obesity'print('BMI value:{}International:{},domestic:{}'.format(BMI,inter,domestic))break
except NameError:print('Please enter the correct value (Arabic numerals)!')
Recommended Posts