06 to determine if the flow control

06 to determine if the flow control

if...

"""
if 条件:# 条件成立的情况下,才会运行if中包含的子代码块
    子代码1
    子代码2
    子代码3
    ...
"""

age = 73
sex = "female"
is_beautiful = True

if age > 16 and age < 20 and sex="female" and is_beautiful:
    print('开始表白。。。')
print("我是if之后的代码,顶级代码")

if...else...

"""
if 条件:# 条件成立的情况下,才会运行if中包含的子代码块
    子代码1
    子代码2
    子代码3
    ...
else:   # 上面条件不成立的情况下,才会运行else包含的子代码块
    子代码1
    子代码2
    子代码3
    ...
"""

age = 73
sex = "female"
is_beautiful = True

if age > 16 and age < 20 and sex="female" and is_beautiful:
    print('开始表白。。。')
else:
    print('你是个好人,我们不合适!')
print("我是if之后的代码,顶级代码")

if...elif...else

  • Streamline examples cited, code, reasonable use of the essence if the conditional statement executed
"""
if 条件1:   # 条件1成立的情况下,才会运行if中包含的子代码块
    子代码1
    子代码2
    子代码3
    ...
elif 条件2:  # 条件2成立的情况下,才会运行该elif中包含的子代码块
    子代码1
    子代码2
    子代码3
    ...
elif 条件3:  # 条件2成立的情况下,才会运行该elif中包含的子代码块
    子代码1
    子代码2
    子代码3
    ... 
else:        # 上面条件不成立的情况下,才会运行else包含的子代码块
    子代码1
    子代码2
    子代码3
    ...
"""

# 示范:   如果:成绩>=90,那么:优秀
#
#        如果成绩>=80且<90,那么:良好
#
#        如果成绩>=70且<80,那么:普通
#
#        其他情况:很差


# 方式一
# 注意内容:input返回值是字符串类型,int只能把纯数字的字符串转化为整型
score = input('请输入您的分数')
if score.isdigest():
    score = int(score) 
    if score >= 90:
       print('成绩的级别为:优秀')
    elif score >= 80 and score < 90:
       print('成绩的级别为:良好')
    elif score >=70 and score <80:
       print('成绩的级别为:普通')
    else:
       print('成绩的级别为:很差')
else:
    print('必须输入纯数字')
    
# 方式二(精简,去掉and后面的条件):完美的展现了if语句执行的精髓 
score = input('请输入您的分数')
if score.isdigest():
    score = int(score) 
    if score >= 90:     # score范围等于90~100才会执行
       print('成绩的级别为:优秀')
    elif score >= 80:   # score范围等于80~89才会执行
       print('成绩的级别为:良好')
    elif score >=70:    # score范围等于70~79才会执行
       print('成绩的级别为:普通')
    else:
       print('成绩的级别为:很差')
else:
    print('必须输入纯数字')

to sum up

  • else it can only be used in the final, elif unrestricted use
  • Judgment order from the implementation of top-down , only the above conditions are not satisfied, you can perform the following judgment .
  • Analyzing the same level, if there is a position determination condition is satisfied, all of which is determined not to be performed later.
  • if the judge sentences, it does not affect the normal execution of the following top-level code.

Guess you like

Origin www.cnblogs.com/yang1333/p/12341054.html