目录
2019/08/01 学习整理
Python基础
流程控制之while循环
一、语法
循环就是一个重复的过程。
语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为:
while 判断条件:
执行语句……
执行语句可以是单个语句或语句块。判断条件可以是任何表达式,任何非零、或非空(null)的值均为true。
当判断条件假false时,循环结束。
执行流程图如下:
如果while 条件为true 将会无限循环
while True:
print('aaa')
'''
aaa
aaa
...
aaa
...
'''
二、while + break
break 的意思是终止掉当前循环,执行其他代码。
while True:
print('1')
print('2')
break
print('3')
'''
1
2
'''
i = 1
while 1: # 循环条件为1必定成立
print i # 输出1~10
i += 1
if i > 10: # 当i大于10时跳出循环
break
"""
1
2
...
10
"""
#当i大于10时跳出循环 故在10时终止
三、while +continue
continue的意思是终止本次循环,直接进入下一次循环
i = 1
while i < 10:
i += 1
if i%2 > 0: # 非双数时跳过输出
continue
print i # 输出双数2、4、6、8、10
四、while循环的嵌套
# 退出内层循环的while循环嵌套
while True:
user_db = 'nick'
pwd_db = '123'
inp_user = input('username: ')
inp_pwd = input('password: ')
if inp_user == user_db and pwd_db == inp_pwd:
print('login successful')
while True:
cmd = input('请输入你需要的命令:')
if cmd == 'q':
break
print(f'{cmd} 功能执行')
else:
print('username or password error')
print('退出了while循环')
# 退出双层循环的while循环嵌套
while True:
user_db = 'nick'
pwd_db = '123'
inp_user = input('username: ')
inp_pwd = input('password: ')
if inp_user == user_db and pwd_db == inp_pwd:
print('login successful')
while True:
cmd = input('请输入你需要的命令:')
if cmd == 'q':
break
print(f'{cmd} 功能执行')
break
else:
print('username or password error')
print('退出了while循环')
使用break退出循环,要对应其while
五、tag控制循环推出
# tag控制循环退出
tag = True
while tag:
user_db = 'nick'
pwd_db = '123'
inp_user = input('username: ')
inp_pwd = input('password: ')
if inp_user == user_db and pwd_db == inp_pwd:
print('login successful')
while tag:
cmd = input('请输入你需要的命令:')
if cmd == 'q':
tag = False
print(f'{cmd} 功能执行')
else:
print('username or password error')
print('退出了while循环')
六、while + else
else会在while没有被break时才会执行else中的代码
count = 0
while count < 5:
print count, " is less than 5"
count = count + 1
else:
print count, " is not less than 5"
'''
0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5
'''
流程控制之for循环
一、语法
Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串。
for循环的语法格式如下:
for iterating_var in sequence:
statements(s)
列表:
name_list = ['nick', 'jason', 'tank', 'sean']
n = 0
while n < 4:
# while n < len(name_list):
print(name_list[n])
n += 1
"""
nick
jason
tank
sean
"""
二、for + break
for循环调出本层循环。
# for+break
name_list = ['nick', 'jason', 'tank', 'sean']
for name in name_list:
if name == 'jason':
break
print(name)
# nick
三、for + continue
for循环调出本次循环,进入下一次循环
# for+continue
name_list = ['nick', 'jason', 'tank', 'sean']
for name in name_list:
if name == 'jason':
continue
print(name)
# nick
# tank
# sean
四、for 循环嵌套
外层循环循环一次,内层循环循环所有的。
# for循环嵌套
for i in range(3):
print(f'-----:{i}')
for j in range(2):
print(f'*****:{j}')
"""
-----:0
*****:0
*****:1
-----:1
*****:0
*****:1
-----:2
*****:0
*****:1
"""
五、for + else
for循环没有break的时候触发else内部代码块。
# for+else
name_list = ['nick', 'jason', 'tank', 'sean']
for name in name_list:
print(name)
else:
print('for循环没有被break中断掉')
"""
nick
jason
tank
sean
"""
for循环没有break中断掉
六、for 循环实现loading
import time
print('Loading', end='')
for i in range(6):
print(".", end='')
time.sleep(0.2)
# Loading......