Python started 6 - Process Control - if Analyzing

Block:

  1 refer to the same block level code, the same number of spaces in the indentation python with (in addition to the top-level code block without any indentation, the other code block is indented four spaces on the basis of the original) identifying a code block with a
  2-level code in the same block in turn run from top to bottom in the order

Note: Get into the habit, do not press "Tab" key to indent, according to four spaces

A. Syntax 1

if 条件: # 条件成立的情况下会运行子代码块
    子代码1
    子代码2
    子代码3
    ...

Example:

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

II. Syntax 2

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

Example:

age = 73
age = 18
sex='female'
is_beautiful=True
if age > 16 and age < 20 and sex=='female' and is_beautiful:
    print('开始表白。。。')
else:
    print('阿姨好,我们不太合适,还是做朋友吧。。。')
print('我是if之后的代码,是顶级代码')

III. Syntax 3

if 条件1: # 条件1成立的情况下会运行子代码块
    子代码1
    子代码2
    子代码3
    ...
elif 条件2: # 条件2成立的情况下会运行子代码块
    子代码1
    子代码2
    子代码3
    ...
elif 条件3: # 条件3成立的情况下会运行子代码块
    子代码1
    子代码2
    子代码3
    ...
......
else:      # 上述条件都不成立的情况下会运行else包含的子代码块
    子代码1
    子代码2
    子代码3
    ...

Example:

如果成绩>=90,那么:优秀

如果成绩>=80且<90,那么:良好

 如果成绩>=70且<80,那么:普通

其他情况:很差

score=input('请输入您的分数进行查询:') # score="abc"
if score.isdigit(): # "99".isdigit()
    score=int(score) # 把纯数字的字符串转换成整型,score=int("99")

    if score >= 90:
        print('成绩的级别为:优秀')
    elif score >= 80:
        print('成绩的级别为:良好')
    elif score >= 70:
        print('成绩的级别为:普通')
    else:
        print('成绩的级别为:很差')

else:
    print('必须输入纯数字')

Guess you like

Origin www.cnblogs.com/xuexianqi/p/12342306.html