流程控制if、while、for

一、if 判断
语法一:
if 条件:
# 以下是上一条if 的子代码块
print(子代码1)
print(子代码2)
print(子代码3)

示例:
# 路边飘过一个生物,要不要表白?
sex = 'female'
age = 18
is beautiful = True

if sex == 'female' and age == 18 and is beautiful:
print('我要表白') # 子代码块代码
print('上一缩进级的代码') # 上一缩进级的代码块
print('上一缩进级的代码')
print('上一缩进级的代码')

if 运行完之后会执行代码下方 上一缩进级 的代码块

语法二:
if 条件:
# 以下缩进级是条件成立时执行的代码块
print(子代码1)
print(子代码2)
print(子代码3)
else:
# 以下缩进级是条件不成立时执行的代码块
print(子代码4)
print(子代码5)
print(子代码6)

示例:
# 路边飘过一个生物,要不要表白?
sex = 'female'
age = 18
is beautiful = True

if sex == 'female' and age == 18 and is beautiful:
print('开始表白')
else:
print('搁置表白')

语法三:
if 条件1:
print(子代码1)
print(子代码2)
print(子代码3)
elif 条件2:
print(子代码4)
print(子代码5)
print(子代码6)
elif 条件3:
print(子代码7)
print(子代码8)
print(子代码9)
else:
print(子代码10)
print(子代码11)
print(子代码12)

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

score = input('please input your score:')
if score >= 90:
print('优秀')
elif score >= 80:
print('良好')
elif score >= 70:
print('普通')
else:
print('很差')

语法四:(if判断的嵌套)
sex = 'female'
age = 18
is beautiful = True
is cuccessful = True

if sex == 'female' and age == 18 and is beautiful:
print('开始表白')
if is cuccessful:
print('在一起')
else:
print('有缘无分')
else:
print('阿姨好')

二、while循环
语法:
while 条件:
print('代码1')
print('代码2')
print('代码3')

示例:
# 循环输入账号密码,并嵌套if判断来打印相输入账号密码后的结果
while True:
name = input('please input your name:')
pwd = input('please input your password')
if name == 'egon' and pwd == 123
print('login successful')
else:
print('username or password error')

结束while循环的两种方式:
方式一:将while条件改为False
在条件改为False时不会立即结束掉循环,而是要等到下一次循环判断条件时才会生效

示例:
# 在输入正确账号密码之后就停止再次输入账号
tag = True # 将True赋值给一个变量,让变量的真假成为条件,通过更改变量的真假来达到改变条件的真假
while tag:
name = input('please input your name')
pwd = input('please input your password')
if name == 'egon' and pwd == '123':
print('login successful')
tag = False
else:
print('username or password error')

方式二:while + break
break一定要放在循环体内,一旦循环体执行到break就会立即结束本层循环

示例:
# 在输入正确账号密码之后就停止再次输入账号
while True:
name = input('please input your name')
pwd = input('please input your password')
if name == 'egon' and pwd == '123':
print('login success')
break
else:
print('username or password error')

中止while的方式:(不是终止)
while + continue:结束本次循环,直接进入下一次循环

示例:
# 除了4,取1-6
count = 1
while count < 7:
if count == 4:
count += 1
'''
因为本次循环到此结束,想要让代码继续运行下去,就必须要对count今天增量赋值
continue下面的增量赋值指令是无法被执行的,但是增量赋值还是要进行
所以加在contine上面
'''
continue
print(count)
count += 1

注意:continue加在循环的最后一行便是无用代码,因为循环最后一行运行完之后自动会重新进行循环,无需continue来中止循环

了解知识:
while + else

while 条件:
代码1
代码2
代码3
else:
在循环结束后,并且在循环没有被break打断过的情况下,才会执行else的代码

示例:
tag=True
while tag:
print(1)
print(2)
print(3)
tag=False
'''
此处利用将条件改为False是循环结束,但是还是会最后运行完接下来的代码的
运行到break,else的代码还是不会被执行
'''
break
else:
print('else的代码')

while循环的嵌套:

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

示例一:
'''
在输错就循环,输对就结束程序的基础上加上:
输对之后进入下一层功能界面,其中的退出功能可直接终止循环
'''
while True:
name = input('please input your name')
pwd = input('please inpunt your password')
if name == 'egon' and pwd == '123':
print('login successful')
while True:
print('''
0 退出
1 取钱
2 转账
3 查询
''')
choice = input('请选择您要执行的操作:')
if choice == '0':
break # 终止此层循环,执行上一缩进级的吓一条代码,即 #1 break
elif choice == '1':
print('取钱')
elif choice == '2':
print('转账')
elif choice == '3':
print('查询')
else:
print('操作有误,请重新选择:')
break # 此处为 #1 break
else:
print('用户名或者密码错误!')

示例二:
# 两个while循环使用相同的可变量True ,只要运行到 '0 退出'就直接封锁所有whjle循环
tag = True
while tag:
name = input('please input your name:')
pwd = input('please input your password:')
if name == 'egon' and pwd == '123':
print('login successgul')
while tag:
print('''
0 退出
1 取款
2 转账
3 查询
''')
choice = input('请选择您要执行的操作:')
if choice == '0':
tag = False
elif choice == '1':
print('取款')
elif choice == '2':
print('转账')
elif choice == '3':
print('查询')
else:
print('操作有误,请重新选择:')
else:
print('用户名或者密码错误!')

三、for循环:
for循环的强大之处在于循环取值

示例:
# 取出列表中的值并打印
list = [a,b,c,d,e]
# 使用while循环取值:
i = 0
while i < 6:
print(list[i]) # i作为整型变量充当列表中的索引存在于 list[i]中,从而达到 顺延索引值 来取值的效果
i += 1
# 使用for循环取值:
for i in list:
'''
# 这层for循环第一次循环时,这一行其实就是 i = list[1],把列表中的第一个值赋值给i
'''
print(i)

# 取出字典中的值并打印
dic={'name':'egon','age':18,'gender':'male'}
for i in dic:
print(i,dic[i])
# 这里的i为字典中的key,故dic[i]则为字典中key_i对应的value

for + continue:中止本次for循环(不是终止)

示例:
# 打印到xx就跳过此次打印
nums=[11,22,33,44,55]
for i in nums:
if i == 44
continue
print(i)

for + break:终止本层for循环

示例:
# 打印到xx就停止打印
nums=[11,22,33,44,55]
for i in nums:
if i == 44:
break
print(i)

for + else :

示例:
names=['egon','kevin1111_dsb','alex_dsb','mac_dsb']
for name in names:
if name == 'kevin1111_dsb'
break # 一旦因break而终止循环的情况,上一个缩进级的else代码便不会被执行
print(name)
else:
print('else测试代码')

for + range():
range的用法
>>> range(1,5)
[1, 2, 3, 4]
>>> for i in range(1,5):
... print(i)
...
1
2
3
4
>>> range(1,5,1)
[1, 2, 3, 4]
>>> range(1,5,2) # 1 3
[1, 3]

for循环的嵌套:

示例:
for i in range(3):
for j in range(4):
print(i,j)
# 无法理解的就看下面的代码,即将range转换为列表方便理解
for i in [0,1,2]: # i=1
for j in [0,1,2,3]: # j=1
'''
# 执行完下层缩进级的for循环,
才会回到上层缩进级的for循环去继续循环未完成的上层循环
'''
print(i,j)
原文地址:https://www.cnblogs.com/yanminggang/p/10572721.html