流程控制

1 分支结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
'''
完整语法:
if 条件1:
代码1
代码2
代码3

elif 条件2:
代码1
代码2
代码3

elif 条件3:
代码1
代码2
代码3

……
else:
代码1
代码2
代码3

'''

1.1 单分支

1
2
3
4
5
print(‘start…’)
inp_name = input(“请输入您的用户名:”)
if inp_name == “egon”:
print(‘ok’)
print(‘end…’)

1.2 双分支

1
2
3
4
5
6
7
print(‘start…’)
inp_name = input(“请输入您的用户名:”)
if inp_name == “egon”:
print(‘ok’)
else:
print(‘error’)
print(‘end…’)

1.3 多分支

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
'''需求:
如果:成绩>=90,那么:优秀
如果成绩>=80且<90,那么:良好
如果成绩>=70且<80,那么:普通
其他情况:很差
'''
score = input(“your score: “)
score = int(score)
if score >= 90:
print(“优秀”)
if 10 > 3:
print(‘ok’)
elif score >= 80 and score < 90:
print(“良好”)
elif score >= 70 and score < 80:
print(“普通”)
else:
print(‘很差’)


#优化版
score = input(“your score: “)
score = int(score)
if score >= 90:
print(“优秀”)
elif score >= 80:
print(“良好”)
elif score >= 70:
print(“普通”)
else:
print(‘很差’)

2 循环结构

2.1 while循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
'''
需求
请输入您的用户名
请输入您的密码
判断 输入的用户名 等于 正确的用户名 并且 输入的密码 等于 正确的密码:
告诉用户登录成功
否则:
告诉用户账号密码错误
'''
inp_name = input(“username: “)
inp_pwd = input(“password: “)

if inp_name == “egon” and inp_pwd == “123”:
print(“登录成功”)
else:
print(“账号密码错误”)

while True:
inp_name = input(“username: “)
inp_pwd = input(“password: “)
if inp_name == “egon” and inp_pwd == “123”:
print(“登录成功”)
break
else:
print(“账号密码错误”)
print(‘end…’)

2.1.1 基本使用

1
2
3
4
5
6
7
“””
语法
while 条件:
代码1
代码2
代码3
“””

2.1.2 死循环

死循环:永远也不结束的循环

1
2
while True:
print('1111')

2.1.3 结束while循环的两种方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#1、break:直接终止本层循环,不会有下一次循环
tag = True
while tag:
print(‘start…’)
break
print(‘end…’)

#2、条件改为假:不会直接终止本层循环,会在下一次循环时终止
tag = True
while tag:
print(‘start…’)
tag = False
print(‘end…’)

#案例
tag = True
while tag:
inp_name = input(“username: “)
inp_pwd = input(“password: “)
if inp_name == “egon” and inp_pwd == “123”:
print(“登录成功”)
# break
tag = False
else:
print(“账号密码错误”)
print(‘xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’)
print(‘end…’)

2.1.4 while+continue:终止本次循环,直接进入下一次

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
i = 0
while i < 5:
if i == 3:
i += 1
continue
print(i)
i += 1 # i=3
# 案例

while True:
inp_name = input("username:")
inp_pwd = input("password:")

if inp_name == "egon" and inp_pwd == "123":
print("登录成功")
break
else:
print("账号密码错误")
# continue # 画蛇添足

print('end…')

2.1.5 while+else

1
2
3
4
5
6
7
8
i = 0
while i < 5:
if i == 3:
continue
print(i)
i+=1
else:
print(‘xxxxxxxxxxxxxxxx’)

2.1.6 用户登录案例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#案例1
while True:
inp_name = input('please input your name: ')
inp_pwd = input('please input your password: ')
if inp_name == "egon" and inp_pwd == "123":
print('login successful')
break
else:
print('username or password error')

# 案例2
count = 0
while True:
inp_name = input('please input your name: ')
inp_pwd = input('please input your password: ')
if inp_name == "egon" and inp_pwd == "123":
print('login successful')
break
else:
print('username or password error')
count +=1
print(count)
if count == 3:
print("输错次数达到了限制")
break

# 案例3
count = 0
while True:
inp_name = input('please input your name: ')
inp_pwd = input('please input your password: ')
if inp_name == "egon" and inp_pwd == "123":
print('login successful')
break
else:
print('username or password error')
count +=1
print(count)
if count == 3:
print("输错次数达到了限制")
break

#案例4
count = 0
while count < 3:
inp_name = input('please input your name: ')
inp_pwd = input('please input your password: ')
if inp_name == "egon" and inp_pwd == "123":
print('login successful')
break
else:
print('username or password error')
count +=1
print(count) # 3
else:
print("输错次数达到了限制")


# 案例5
count = 0
while True:
inp_name = input('please input your name: ')
inp_pwd = input('please input your password: ')
if inp_name == "egon" and inp_pwd == "123":
print('login successful')
while True:
print("""
0 退出
1 取款
2 存款
3 转账
""")
choice = input("请输入命令编号: ")
if choice == "0":
break
elif choice == "1":
print("============正在取款============")
elif choice == "2":
print("============正在存款============")
elif choice == "3":
print("============正在转账============")
else:
print("============输入错误============")
break
else:
print('username or password error')
count +=1
print(count)
if count == 3:
print("输错次数达到了限制")
break

2.2 for循环

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
ll = [[111,222],[333,444],[555,6666]]
for x,y in ll: # x,y=[111,222]
print(x,y)

#1、for + break
for x in [111,222,33,444,555]:
if x == 33:
break
print(x)

#2、for + continue
for x in [111,222,33,444,555]:
if x == 33:
continue
print(x)

#3、for + else
for x in [111,222,33,444,555]:
if x == 33:
# break
continue
print(x)
else:
print('xxxxxxxxxxxxxxx')#for循环执行失败,则执行此段代码

#4、for+range
ll = [111,222,33,444,555]
for i in range(len(ll)):
print(ll[i])

#5、for+enumerate
ll = [111,222,33,444,555]
for i,v in enumerate(ll): # i,v=(0, 111)
print(i,v)

# 6、for循环嵌套
for i in range(3): # i=2
print('=================>外层循环', i)
for j in range(5):
print("内层循环", j)
"""
=================>外层循环 0
内层循环 0
内层循环 1
内层循环 2
内层循环 3
内层循环 4
=================>外层循环 1
内层循环 0
内层循环 1
内层循环 2
内层循环 3
内层循环 4
=================>外层循环 2
内层循环 0
内层循环 1
内层循环 2
内层循环 3
内层循环 4
"""