zoukankan      html  css  js  c++  java
  • 流程控制之 if 判断

    流程控制之if

    if 条件语句

    if 条件:
        代码1
        代码2
        ...
    # 代码块:同一缩进级别的代码,例如代码1和代码2是相同缩的代码,相同缩进的代码会自上而下的运行.
    # if + 条件的语句,只有在条件成立时才会执行代码块
    
    gender = 'female'
    age = 21
    is_beautiful = True
    if gender == 'female' and age == 21 and is_beautiful:
        print('开始表白!')
        
    # 开始表白
    

    if...else 条件语句

    if 条件:
        代码1
        代码2
        ...
    else 条件:
        代码1
        代码2
        ...
    # 当 if 判断的条件不成立的时候,会执行 else 当中的代码块.
    
    gender = 'female'
    age = 30
    is_beautiful = False
    if gender == 'female' and age == 21 and is_beautiful:
        print('开始表白!')
    else:
        print('没有感觉!')
    
    # 没有感觉
    

    if...elif...else 条件语句

    if 条件:
        代码1
        代码2
        ...
    elif 条件:
        代码1
        代码2
        ...
    else:
        代码1
        代码2
        ...
    # 当 if/elif/else 中有一条成立时只会执行这一条当中的代码块,另外两条都不会执行
    
    gender = 'female'
    age = 30
    is_beautiful = True
    if gender == 'female' and age == 21 and is_beautiful:
        print('开始表白!')
    elif gender == 'female' and is_beautiful:
        print('考虑下!')
    else:
        print('没有感觉!')
    

    if 的嵌套

    gender = 'female'
    age = 30
    is_beautiful = False
    if gender == 'female' and age == 21:
        print('开始表白!')
        if is_beautiful:
            print('加微信!')
        else:
            print('不好意思,打扰了!')
    else:
        print('没有感觉!')
    
    千里之行,始于足下。
  • 相关阅读:
    ZOJ 3327 Friend Number
    ZOJ 3324 Machine
    2010-2011 ACM-ICPC, NEERC, Southern Subregional Contest C Explode 'Em All
    UVA 12594 Naming Babies
    POJ 3709 K-Anonymous Sequence
    POJ 1180 Batch Scheduling
    POJ 1160 Post Office
    HDU 3516 Tree Construction
    HDU 3045 Picnic Cows
    UVALive 5097 Cross the Wall
  • 原文地址:https://www.cnblogs.com/jincoco/p/11121682.html
Copyright © 2011-2022 走看看