五、流程控制之if 判断

1、什么是if判断?

  判断一个条件如果成立则做什么不成立则做什么

2、为何要有if判断?

  为了让计算机能想人一样具有判断的能力

3、如何用if判断?

  if语法1:

if 条件1:
    code
    code
    code
    ......
age=18
if age==18
    print('你好美女')

  if语法2:

if 条件:
    code
    code
    code
    ......
else:
    code
    code
    code
    ......

age=18
sex='female'
species='human'
beautiful=Ture

if age>=18 and age<25 and sex=='female' \
        and species='human' and beautiful:
    print('开始表白。。。')
else:
    print('不好意思,打扰了')

  if语法3:

if 条件1:
    if 条件2:
        code1
        code2
        code3
        ......
    code1
    code2
    ......

age=18
sex='female'
species='human'
beautiful=True
successful=True

if age>18 and age<25 and sex=='female'\
        and species='human' and beautiful:
    print('开始表白。。。')
    if successful:
        print('在一起')
    else:
        print('不好意思,打扰了')
else:
    print('不好意思,认错人了')

  if语法4:

if 条件1:
    子代码块1
elif 条件2:
    子代码块2
elif 条件3:
    子代码块3
elif 条件4:
    子代码块4
elif 条件5:
    子代码块5
elif 条件6:
    子代码块6
else:
    子代码块7

score=int(input('your score>>:'))
if score>=90:
    print('excellent')
elif score>=80:
    print('well')
elif score>=70:
    print('general')
else:
    print('bad')

 

 

  

posted @ 2018-09-14 14:50  Maple_feng  阅读(704)  评论(0编辑  收藏  举报