一.循环语句
1.while 循环- 当我们在 python 中需要重复执行一些动作的时候,这时我们就要用到循环
- while 循环的结构,当条件成立的时候,就会执行里面的代码
- while 循环不断的运行,直到指定的条件不满足为止。
- 注意死循环
while 条件成立:
执行内容
number = 1 # 指定从1开始数
while number <= 5: # 只要number小于或等于5 就接着运行这个循环
print(number) # 打印循环的值
number += 1 # 再给number值 + 1
# 由此类推, 一旦number大于5,循环就停止,整个程序也将到此结束
# 输出结果如下:
1
2
3
4
5
"""死循环/无限循环"""
#例1
count = 0
while count == 0:
num = input('请输入编号:')
#例2
A = True
while True:
print ("ok")
"""练习1"""
#奇偶分离
list1 = [1,2,3,4,5,6,7,8,9,10]
G = []
O = []
while len(list1)>0:
num1 = list1.pop()
if num1 % 2 == 0:
G.append(num1)
else:
O.append(num1)
print (G)
print (O)
"""
"""
# 用while循环列出1-100数字
num = 0
while num < 101:
print (num)
num += 1
"""
"""
# 用while循环列出1-100中的所有偶数
num = 1
while num < 101:
if num % 2 == 0:
print (num)
num += 1
"""
"""
#取出0-100之间的所有偶数,放到列表list1中
num = 0
list1 = []
while num <= 100:
if num % 2 == 0:
list1.append(num)
num += 1
print (list1)
num = 0
list1 = []
while num <= 100:
list1.append(num)
num += 2
print (list1)
num = 0
list1 = []
while num <= 100:
list1.insert(0,num)
list1.sort()
num += 2
print (list1)
num = 0
i = 0
list1 = []
while num <= 100:
list1.insert(i,num)
i += 1
num += 2
print (list1)
"""
"""
# 用while循环求出1-100中的所有偶数之和
num = 1
sum = 0
while num < 101:
if num % 2 == 0:
sum += num
num += 1
print (sum)
"""
"""练习2"""
#coding=utf-8
#模拟登录操作
dic = {'zhangsan':'123456','lisi':'234567','wangwu':'345678'}
A = True
while A:
user = input("请输入你的账户:")
if user in dic.keys():
while A:
pwd = input("请输入密码:")
if pwd == dic.get(user):
print ("登录成功")
A = False
else:
print ("密码错误")
else:
print ("用户名不存在")
"""练习3"""
#coding=utf-8
#注册手机号为1开头的11位数字,密码大于6小于18位数,确认密码,将账号密码加入字典dic中
dic = {}
A = True
while A:
phone = input("请输入手机号:")
if len(phone) == 11 and phone.isdigit() == True and phone.startswith('1'):
while A:
pwd = input("请输入密码:")
if 6 < len(pwd) < 18:
repwd = input("请确认密码:")
if pwd == repwd:
print ("恭喜你,注册成功!")
dic.setdefault(phone,pwd)
print (dic)
A = False
else:
print ("两次密码输入不一致!")
else:
print ("请输入7到17位密码!")
else:
print ("请输入1开头的11位数字的手机号!")
line = 1
while line <= 4:
print("*" * line)
line += 1
#当num输入为1时,结果如下:
---> #
---> ##
---> ###
---> ####
2.break 退出(终止)循环
- 要立即
,不再运行循环中余下的代码,也不管条件测试的结果如何,可退出 while 循环
使用 break 语句
- 注意: 在任何 Python 循环中都可使用 break 语句。例如,可使用 break 语句来退出遍历列表或字典的 for 循环。
num = 0
while num<10:
if num == 5:
break
print(num)
num += 1
#输出内容是
0
1
2
3
4
A = True
while True:
print ("ok")
break
username = "zhangsan"
password = "123456"
for i in range(3):
user = input("User:")
pwd = input("Pwd:")
if user == username and pwd == password:
print("Welcome %s login..." % user)
break #打断,跳出循环
else:
print("Username or password was error !")
else: #只要上面的 for 循环正常执行完毕,中间没用被打断,就会执行 else 语句
exit(u"错误次数过多,请稍后再试...")
username = "zhangsan"
password = "123456"
count = 0
while count < 3:
user = input("User:")
pwd = input("Pwd:")
if user == username and pwd == password:
print("Welcome %s login..." % user)
break #打断,跳出循环
else:
print("Username or password was error !")
count += 1
else: #只要上面的 while 循环正常执行完毕,中间没用被打断,就会执行 else 语句
exit(u"错误次数过多,请稍后再试...")
3.continue 退出当前循环
-
continue 语句用来告诉 Python 跳过当前循环的剩余语句, 然后继续进行下一轮循环;continue 语句用在 while 和 for 循环中
-
Python continue 语句跳出本次循环, 而 break 跳出整个循环
#小于 5 不打印(即打印出5,6,7,8,9)
for i in range(10):
if i < 5:
continue
print(i)
num = 0
while num < 10:
num += 1
if num == 3:
continue # 当程序运行到 continue 时,跳出当前次循环,从新执行 num += 1
print(num)
num = 0
while num<10:
num += 1
if num%2 == 0:
continue
print(num)
#输出内容是
1
3
5
7
9
var = 10
while var > 0:
var = var - 1
if var == 5 or var == 8:
continue
print('当前值:', var)
print('Bye')
作者:多测师高级讲师_郑sir
微信:ZhengYing8887
出处:https://www.cnblogs.com/ZhengYing0813/
备注:本文版权归作者所有,欢迎转载和添加作者微信探讨技术,但未经作者同意必须在文章页面给出原文链接,否则保留追究法律责任的权利。