流程控制(if、while、for)

发布时间:2019-05-24 21:54:08编辑:auto阅读(2114)

    流程控制

    一、if判断

    # 1、语法一
    if 条件:
    #条件成立时执行的子代码块`
    代码1
    代码2
    代码3
    # 示例:
    sex='female'
    age=18
    is_beautiful=True
    if sex == 'female' and age > 16 and age < 20 and is_beautiful:
       print('开始表白。。。')
    print('other code1...')
    print('other code2...')
    print('other code3...')

    # 2、语法二
    if 条件:
       # 条件成立时执行的子代码块
       代码1
       代码2
       代码3
    else:
       # 条件不成立时执行的子代码块
       代码1
       代码2
       代码3
    # 示例:
    sex='female'
    age=38
    is_beautiful=True
    if sex == 'female' and age > 16 and age < 20 and is_beautiful:
       print('开始表白。。。')
    else:
       print('阿姨好。。。')
    print('other code1...')
    print('other code2...')
    print('other code3...')

    # 3、语法三:
    if 条件1:
       if 条件2:
           代码1
           代码2
           代码3
    # 示例:
    sex='female'
    age=18
    is_beautiful=True
    is_successful=True
    height=1.70
    if sex == 'female' and age > 16 and age < 20 and is_beautiful \
           and height > 1.60 and height < 1.80:
       print('开始表白。。。')
       if is_successful:
           print('在一起。。。')
       else:
           print('什么爱情不爱情的,爱nmlgb的爱情,爱nmlg啊.')
    else:
       print('阿姨好。。。')
    print('other code1...')
    print('other code2...')
    print('other code3...')

    # 4、语法四:
    if 条件1:
       代码1
       代码2
       代码3
    elif 条件2:
       代码1
       代码2
       代码3
    elif 条件3:
       代码1
       代码2
       代码3
    .......
    else:
       代码1
       代码2
       代码3

关键字