Python基础——条件判断

条件判断的目的:

可以让计算机自动化很多任务

在Python中主要通过if语句实现循环,如果if后面的结果为true就执行if后面的语句,反之则不执行,if可以与else配对使用

if语句的执行特点是从上往下执行,如果判断某个为true就执行

if 后面的条件只要是非零数值、非空字符串、非空list

可以通过input()读取用户的输入:

注意:

  1. input()返回的数据类型是str,str不能直接与整数比较,必须先把str转换成整数
  2. int()函数如果发现一个字符串并不似合法的数字时就会报错

代码:通过if-elif语句判断体重在BMI的哪个范围
 

def BMI(weigth,height):
    bmi = weigth/(height*height)
    return bmi

weight,height = eval(input('请输入体重,身高:'))

bim = BMI(weight,height)
print(bim)
if bim < 18.5:
    print('过轻')
elif bim >18.5 and bim < 25 :
    print('正常')
elif bim >25 and bim<28 :
    print('过重')
elif bim > 28 and bim < 32 :
    print('肥胖')
else:
    print('严重肥胖')

 

猜你喜欢

转载自blog.csdn.net/qq_39059714/article/details/84393263