determining if the flow control

Process control purposes if

if conditional statement

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 conditional statements

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 conditional statements

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 nesting

gender = 'female'
age = 30
is_beautiful = False
if gender == 'female' and age == 21:
    print('开始表白!')
    if is_beautiful:
        print('加微信!')
    else:
        print('不好意思,打扰了!')
else:
    print('没有感觉!')

Guess you like

Origin www.cnblogs.com/jincoco/p/11121682.html