流程控制之if判断

1.什么是if判断

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

2.为何要有if判断

       让计算机能够像人一样具有判断的能力

3.如何用if判断

'''

#语法1:

'''

if 条件1:

    code1

    code2

    code3

    .......

'''

age=18

if age != 18:

    print('你好啊小伙子')
    print('加个微信吧...')

print('other code...')

# 语法2:

if 条件:

    code1

    code2

    code3

  ......

else:

    code1

    code2

    code3

    ......

#

age=22

sex = 'male'

x='dd'

is cool = True

if age>16 and age<25 and

x='dd' and is cool:

    print('你好')

else:

    print('再见')

 

# 语法3

 if 条件1:

    if 条件2:

        code1

        code2

        code3

    code2

    code3

      .......

age = 18

sex = 'hemale'

shencai = 'good'

is_beautiful = True

is_rich = True

if age >16 and age< 25 and sex = 'hemale'

and shencai = 'good' and is_beautiful:

    print ('表白')

    if is_rich: 

        print('在一起')

    else:

        print('你是谁')

else:

    print('8888888')

# 语法4

if 条件1:

        子代码块1

elif 条件2:

        子代码块2

elif 条件3:

        子代码块3

elif 条件4:

        子代码块4

.............

else:

        子代码块5

#

score = input('your score:')

score = int(score)

if score >= 90:

    print('A')

elif score >= 80:

    print('B')

elif score >= 70:

    print('C')

else:

    print('laji')

原文地址:https://www.cnblogs.com/xiamenghan/p/9648874.html