流程控制之while循环

语法

循环就是一个重复的过程,人需要重复做某项工作,那么计算机也一样。当ATM验证失败,那么计算机就会让我们再输入一次密码。这时候我们说的循环就是while循环,while循环即条件循环。

while 条件:
代码... while True:
print("hello world")
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')
else:
print('username or password error')

上述代码就是模拟一个用户登录,但无论用户有没有登录成功,都会继续循环。因为条件是True

while+break

break的意思是终止掉当层循环,执行其他代码

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')
break
else:
print('username or password error') print('退出了while循环')
username:nick
password:123
login successful
退出了while循环

while+continue

continue的意思是终止本次循环,直接进入下一次循环

n = 1
while n < 10:
if n == 8:
# n += 1 # 如果注释这一行,则会进入死循环
continue
print(n)
n += 1

continue不能加载循环体的最后一部执行的代码,因为这样做毫无意义

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') 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循环')
username: nick
password: 123
login successful
请输入你需要的命令:q
退出了while循环

while循环的控制条件

# 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循环

while+else

while + else:else会在while没有被break时才会执行代码

# while+else
n = 1
while n < 3:
print(n)
n += 1
else:
print('else会在while没有被break时才会执行else中的代码')
1
2
else会在while没有被break时才会执行else中的代码

流程控制之for循环

语法

当我们给出一个列表,把其中的元素取出来用while就能实现,但如果是字典呢,这是我们就要用到for循环可以把key和value同时都取出来,for循环作用于容器型数据。

person={'name':'nick','gender':'nan'}
for key,value in person.items():
print(key,value)
name nick
gender nan

for+break

跳出本层循环

# for+break
name_list = ['nick', 'jason', 'tank', 'sean']
for name in name_list:
if name == 'jason':
break
print(name)
nick

for+continue

跳出本次循环,直接进入下一次循环

# 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循环中loding......

import time

print('Loading', end='')
for i in range(6):
print(".", end='')
time.sleep(0.2)

pycharm中无法实现,jupyter中可以

loding......

最新文章

  1. poj3122-Pie(二分法+贪心思想)
  2. Unicode与JavaScript详解
  3. maven 环境搭建
  4. HDU 2083 简易版之最短距离 --- 水题
  5. 在MAC平台下编译Ngnix ,由于MD5算法不能编译通过 解决办法
  6. Spring properties dependency checking
  7. Android之发送短信和接收验证码
  8. C++中this指针的使用方法.
  9. crossdomain.xml跨越
  10. TensorFlow实验环境搭建
  11. windows bat 批处理 执行 for 循环无法执行?
  12. Keep面经汇总
  13. Codeforces 1131F Asya And Kittens (构造)【并查集】
  14. shadows
  15. Angular 快速入门
  16. 【POJ3250】Bad Hair Day 单调栈
  17. 2018.10.16 uoj#340. 【清华集训2017】小 Y 和恐怖的奴隶主(矩阵快速幂优化dp)
  18. linux提权辅助工具(三):privchecker.py
  19. ZooKeeper教程(一)----Centos7下安装ZooKeeper(单机版)
  20. uvalive 3231 Fair Share 公平分配问题 二分+最大流 右边最多流量的结点流量尽量少。

热门文章

  1. sublime中替换成换行
  2. springboot(七).springboot整合jedis实现redis缓存
  3. Java数组分配内存空间
  4. mysql数据库的索引
  5. 21.栈的压入、弹出序列 Java
  6. JS各循环的差别
  7. python获得坐标系信息
  8. 如何配置git send-email相关的邮箱信息?
  9. ossfs挂载oss到ECS本地并设置权限
  10. JavaScript如何封装插件