python_4

关于if else

python中

if 变量的条件 :

  执行条件语句;

else:

  执行条件语句;

if 变量的条件1:

  执行条件语句;

elif变量的条件2:

  执行条件语句;

elif 变量的条件3:

  执行条件语句;

else:

  执行条件语句4;

if语句执行有个特点,它是从上往下判断,如果在某个判断上是True,把该判断对应的语句执行后,就忽略掉剩下的elifelse,所以,请测试并解释为什么下面的程序打印的是teenager

if x:
    print('True')



input()返回的数据类型是strstr不能直接和整数比较,必须先把str转换成整数。Python提供了int()函数来完成这件事


s = input('birth: ')
birth = int(s)
if birth < 2000:
    print('00前')
else:
    print('00后')


作业


height = 1.75
weight = 80.5
BMI = weight/(height*height);
if BMI>32:
print("serious heavier %.1f" %BMI);
elif BMI>28 and BMI<32:
print("obisity %.1f" %BMI);
elif BMI>25 and BMI<28:
print("overweight %.1f" %BMI);
elif BMI>18.5 and BMI<25:
print("normal %.1f" %BMI);
elif BMI<18.5:
print("too light %.1f" %BMI);
else:
print("what the hell???")


上面材料引用廖雪峰!

本博客所刊登的所有内容,

未经创作者合法授权,禁止一切形式转载,违者必究

原文地址:https://www.cnblogs.com/jialikesensi/p/7674249.html