2019.08.01学习整理
流程控制之while循环
1.语法
我们的wile循环,while循环又称为条件循环
while 条件:
code 1
code 2
code 3
...
while True:
print('*1'*100)
print('*2'*100)
while True:
user_db = 'ming'
pwd_db = '123'
inp_user = input('username: ')
inp_pwd = input('password: ')
if inp_user==user_db and inp_pwd==pwd_db:
print('login successful')
else:
print('username or password error')
上述代码用户密码输对了,他也会继续输入。
2. while+break
break的意思是终止掉当前层的循环,执行其他代码。
while True:
user_db = 'ming'
pwd_db = '123'
inp_user = input('username: ')
inp_pwd = input('password: ')
if inp_user==user_db and inp_pwd==pwd_db:
print('login successful')
break
else:
print('username or password error')
username : ming
password: 123
login successful
退出了while循环
3. while+continue
continue的意思是终止本次循环,直接进入下一次循环
n = 0
while n < 5:
n += 1
if n == 3:
continue
print(n)
1
2
4
5
continue不能加在循环体的最后一步执行的代码,因为代码加上去毫无意义,如下所示
while True:
if 条件1:
code1
code2
code3
...
else:
code1
code2
code3
...
continue
4. while循环的嵌套
ATM密码输入成功还需要进行一系列的命令操作,比如取款,比如转账。并且在执行功能结束后会退出命令操作的功能,即在功能出执行输入q会退出输出功能的while循环并且退出ATM程序。
# 退出内层循环的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')
# 退出双层循环的while循环嵌套
while True:
user_db = 'ming'
pwd_db = '123'
inp_user = input('username: ')
inp_pwd = input('password: ')
if inp_user == user_db and inp_pwd == pwd_db:
print('login successful')
while True:
cmd = input('请输入你需要的命令:')
if cmd == 'q':
print(f'{cmd}功能执行')
break
break
else:
print('username or password error')
print('退出了while循环')
username: ming
password: 123
login successful
请输入你需要的命令:q
q功能执行
退出了while循环
5. 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循环')
username: nick
password: 123
login successful
请输入你需要的命令:q
q 功能执行
退出了while循环
6. while+else
while+else:else会在while没有被break时才会执行else中的代码
# while+else
n = 1
while n < 3:
print(n)
n += 1
else:
print('else会在while没有被break时才会执行else中的代码')
1
2
else会在while没有被break时才会执行else中的代码
流程控制之for循环
1.语法
- 字典也有取多个值的需求,字典可能有while循环无法使用了,这个时候可以使用我们的for循环
name_list = ['nick', 'jason', 'tank', 'sean']
for item in name_list:
print(item)
nick
jason
tank
sean
- for循环的循环次数受限于容器类型的长度,而while循环的循环次数需要自己控制。for循环也可以按照索引取值。
# for循环按照索引取值
name_list = ['nick', 'jason', 'tank', 'sean']
# for i in range(5): # 5是数的
for i in range(len(name_list)):
print(i, name_list[i])
0 nick
1 jason
2 tank
3 sean
2. for+break
for循环调出本层循环。
# for+break
name_list = ['nick', 'jason', 'tank', 'sean']
for name in name_list:
if name == 'jason':
break
print(name)
nick
3. for+continue
or循环调出本次循环,进入下一次循环
# for+continue
name_list = ['nick', 'jason', 'tank', 'sean']
for name in name_list:
if name == 'jason':
continue
print(name)
nick
tank
sean
4. 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
5. 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中断掉
6. for循环实现loading
import time
print('Loading', end='')
for i in range(6):
print(".", end='')
time.sleep(0.2
Loading......