Introduction to Python Programming (017) - Selecting Structured Programming

Introduction to Python Programming (017) - Selecting Structured Programming

The selection structure in the program is also called the judgment structure, which selects and executes different code fragments according to conditions. There are three main forms of selection structures in Python: if statements, if…else statements, and if…elif…else statements.

1. if statement

The syntax format of the if statement is as follows:

if 表达式:
    语句块

illustrate:

(1) Expression: It can be a comparison expression or a logical expression. If the value of the expression is True, the statement block is executed; if the value of the expression is False, the statement block is skipped and subsequent statements are executed.

(2) Commonly used comparison operators in expressions are as shown in the following table:

comparison operator > >= == != < <=
effect more than the greater than or equal to equal not equal to less than less than or equal to

Logical Operators:

Logical Operators and or not
effect logical AND logical or logical negation

(3) Python uses code indentation and colons to distinguish levels between codes. The colon at the end of the if expression line and the indentation of the next line (usually 4 spaces are used as an indentation amount) indicate the beginning of a code block, and the end of the indentation indicates the end of a code block. When using if statements, you must strictly follow the indentation rules for coding.

Example 1:

test = input("请输入转账金额:")
if int(test) > 5000:
    print("单笔转账金额不能超过5000元")
    
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
请输入转账金额:8000
单笔转账金额不能超过5000

Example 2: Indentation problem

test = input("请输入转账金额:")
if int(test) > 5000:
    print("单笔转账金额不能超过5000元")
print("程序结束")  # 该行代码不属于if语句的代码块
    
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
请输入转账金额:2000
程序结束

Example 3:

num = int(input("请输入一个整数:"))
if num > 0 and num % 2 == 0:
    num **= 2
print(num)
    
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
请输入一个整数:4
16
>>> 
请输入一个整数:15
15

2. if…else statement

The syntax format of the if…else statement is as follows:

if 表达式:
    语句块1
else:
    语句块1

Description: If the condition specified by the expression is met, the statement block after if is executed, otherwise, the statement block after else is executed.

Example 1:

num = int(input("请输入一个整数:"))
if num % 2 == 0:
    print("你输入了一个偶数")
else:
    print("你输入了一个奇数")
print("程序结束")
    
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
请输入一个整数:15
你输入了一个奇数
程序结束
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入一个整数:200
你输入了一个偶数
程序结束

Example 2: Nesting of if…else statements

num = float(input("请输入一个数:"))
if num > 0:
    print("你输入了一个正数")
else:
    if num < 0:
        print("你输入了一个负数")
    else:
        print("你输入的数是0")
print("程序结束")
    
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
请输入一个数:100
你输入了一个正数
程序结束
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入一个数:-2.5
你输入了一个负数
程序结束
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入一个数:0
你输入的数是0你
程序结束

3. if…elif…else statement

The syntax format of the if…elif…else statement is as follows:

if 表达式1:
    语句块1
elif:
    语句块2
elif:
    语句块3
....
else:
    语句块n

Description: The if...elif...else statement is a multi-branch selection statement. When the value of a certain expression is true, the subsequent statement block is executed; if the value of the expression is plus, the subsequent statement block is skipped and executed. Evaluation of the next elif expression. If all expressions are not true, the statement block following else is executed.

Example 1: Determine whether a number is positive, negative or 0

num = float(input("请输入一个数:"))
if num > 0:
    print("你输入了一个正数")
elif num < 0:
    print("你输入了一个负数")
else:
    print("你输入数是0")
print("程序结束")
    
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
请输入一个数:5
你输入了一个正数
程序结束
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入一个数:0
你输入数是0
程序结束
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入一个数:-12.5
你输入了一个负数
程序结束

Example 2: Enter the score and determine its grade (excellent, good, medium, passing, failing)

num = float(input("请输入考试成绩:"))
if num > 100 or num < 0:
    print("成绩输入错误!")
elif num >=90:
    print("优秀")
elif num >=80:
    print("良好")
elif num >=70:
    print("中等")
elif num >=60:
    print("及格")
else:
    print("不及格")
print("程序结束")
    
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
请输入考试成绩:500
成绩输入错误!
程序结束
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入考试成绩:100
优秀
程序结束
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入考试成绩:25
不及格
程序结束
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入考试成绩:60
及格
程序结束
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入考试成绩:80
良好
程序结束
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入考试成绩:75
中等
程序结束

4. Combining multi-branch selection statements with dictionaries

For some if...elif...else statements whose statement blocks are relatively simple (the statement block has only one output), the expression and the output content of the statement block can be saved in the dictionary.

For example:

dict1 = {
    
    "1":"查看","2":"添加","3":"删除","4":"更新","0":"退出系统"}
num = input("请输入你的操作类型(0-4):")
select = dict1.get(num,"a")
if select != "a":
    print("你选择的操作类型为:",select)
else:
    print("不存在你要选择的操作")
print("程序结束")
    
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
请输入你的操作类型(0-4)0
你选择的操作类型为: 退出系统
程序结束
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入你的操作类型(0-4)1
你选择的操作类型为: 查看
程序结束
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入你的操作类型(0-4)2
你选择的操作类型为: 添加
程序结束
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入你的操作类型(0-4)3
你选择的操作类型为: 删除
程序结束
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入你的操作类型(0-4)4
你选择的操作类型为: 更新
程序结束
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入你的操作类型(0-4)5
不存在你要选择的操作
程序结束

5. Logical operator and

You can use the and operator to judge multiple conditions. Only when multiple conditions are met at the same time can the statement block after the if be executed.

For example:

score1 = float(input("请输入笔试成绩:"))
score2 = float(input("请输入机试成绩:"))
if score1 >= 60 and score2 >= 60:
    print("计算机等级考试通过!")
else:
    print("计算机等级考试没通过!")
print("程序结束")
    
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
请输入笔试成绩:80
请输入机试成绩:90
计算机等级考试通过!
程序结束
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入笔试成绩:50
请输入机试成绩:90
计算机等级考试没通过!
程序结束

6. Logical operator or

You can use the or operator to judge multiple conditions. As long as one of the multiple conditions is met, the statement block after the if is executed.

For example:

java_score = float(input("请java语言考试成绩:"))
python_score = float(input("请python语言考试成绩:"))
go_score = float(input("请go语言考试成绩:"))
if java_score >= 60 or python_score >= 60 or go_score >= 60:
    print("计算机等级考试通过!")
else:
    print("计算机等级考试没通过!")
print("程序结束")
    
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
请java语言考试成绩:50
请python语言考试成绩:0
请go语言考试成绩:0
计算机等级考试没通过!
程序结束
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请java语言考试成绩:88
请python语言考试成绩:0
请go语言考试成绩:0
计算机等级考试通过!
程序结束

7. Nesting of if…else statements

The nested form of if…else statements is as follows:

if 表达式1:
    if 表达式2: 
        语句块1
    else:
        语句块2
else:
    if 表达式3: 
        语句块3
    else:
        语句块4

For example:

num = float(input("请输入一个数:"))
if num >= 0:
    if num > 0:
        print(num,"是一个正数。")
    else:
        print(num,"是0。")
else:
    print(num,"是一个负数。")
        
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
请输入一个数:0
0.00>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入一个数:12.5
12.5 是一个正数。
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入一个数:-2
-2.0 是一个负数。

The above code can be written in the following form:

num = float(input("请输入一个数:"))
if num > 0:
    print(num,"是一个正数。")
else:
    if num < 0:
        print(num,"是一个负数。")
    else:
        print("输入的数是0。")
        
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
请输入一个数:1.2
1.2 是一个正数。
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入一个数:-2.58
-2.58 是一个负数。
>>> 
===================== RESTART: C:\Python\Python38\First.py =====================
请输入一个数:0
输入的数是0

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/132194109