While循环
for循环用于针对集合的每个元素的一个代码块,而while循环不断的运行,直到指定的条件不满足为止。
使用标志
- 在要求很多条件都满足才继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态,这个变量称为标志。
- 你可让程序在标志为True是继续运行,并在任何事件导致标志的值为False时让程序停止运行。这样,在while语句中就只需检查一个条件—标志的值是否为True。
例如:
prompt="\nTell me something,and I will reoert it back to you"
prompt+="\nEnter'quit' to end the program."
active=True
while active:
massage = input(prompt)
if massage=='quit': #当用户输入quit时,循环终止
active = False
else:
print(massage)
// 输出结果
Tell me something,and I will reoert it back to you
Enter'quit' to end the program.hello world
hello world
Tell me something,and I will reoert it back to you
Enter'quit' to end the program.quit
使用break退出循环
break语句用于控制程序流程,可使用他控制哪些代码执行,哪些代码不执行,从而让程序按你的要求执行你要执行的代码。break语句立即跳出循环,不在运行余下的代码。
例如:
prompt="请输入你去过的城市(输入quit退出):"
citys=[]
active=True
while True:
city=input(prompt)
if city=="quit":
break
else:
citys.append(city)
print("你去过这些城市",citys)
//输出结果
请输入你去过的城市(输入quit退出):广州
你去过这些城市 ['广州']
请输入你去过的城市(输入quit退出):上海
你去过这些城市 ['广州', '上海']
请输入你去过的城市(输入quit退出):北京
你去过这些城市 ['广州', '上海', '北京']
请输入你去过的城市(输入quit退出):quit
- 在任何python循环中都可使用break语句。
- 以while True打头的循环将不断运行,直到遇到break语。
在循环中使用continue
要返回到循环开头,并根据条件测试结果决定是否继续执行循环,使用continue语句。
例如:打印奇数(1~10)
current_number=0
while current_number < 10:
current_number+=1
if current_number % 2 == 0:
continue
print(current_number)
// 输出结果
1
3
5
7
9
- break和continue的区别:break终止当前循环,continue使循环跳过本次循环,开始下一次的循环
使用while循环来处理列表和字典
- for循环是一种遍历列表的有效方法,但在for循环中不应修改列表,否则将导致python难以跟踪其中的元素。
- 要在遍历列表的同时对其进行修改,可使用while循环
在列表之间移动元素
网站用户中有一群新注册但还未验证的用户,如何验证这些用户之后转移到已验证的用户列表中。使用while循环,在验证用户的同时将其从未验证用户列表中提取出来,再加入到另一个已验证的用户列表中去。
如:
# 首先,创建一个待验证的用户列表
# 创建一个存储已验证的用户空列表
unconfirmed_users=["aliec","brian","candace"]
confirmed_users=[]
# 验证每个用户,直到没有未验证的用户为止
# 将每个经过验证的用户都移到已验证的用户列表中
while unconfirmed_users:
current_user=unconfirmed_users.pop()
print("verifying user:",current_user)
confirmed_users.append(current_user)
#显示所有已验证的用户
print("\nThe following users have beenconfirmed:")
for confirmed_user in confirmed_users:
print(confirmed_user)
// 输出结果
verifying user: candace
verifying user: brian
verifying user: aliec
The following users have beenconfirmed:
candace
brian
aliec
删除包含特定值的所有元素
要删除这些元素,可不断运行一个while循环
例如:
ptes=["dog","cat","dog","goldfish","cat","rabbit","cat"]
print(ptes)
while "cat" in ptes:
ptes.remove("cat")
print(ptes)
// 输出结果
['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
['dog', 'dog', 'goldfish', 'rabbit']
使用用户输入填充字典
使用while循环提示用户输入任意数量的信息。
respomses={}
#设置一个标志,指出调查是否继续
polling_active=True
while polling_active:
# 提示输入被调查者的名字和回答
name=input("\nwhat in your name?")
respose=input("which mounrain would you like ti climb someday?")
# 将答卷存储在字典中
respomses[name]=respose
#看看是否还有人要参与调查
repeat= input("would you like to let another person respond?(yes/no)")
if repeat=="no":
polling_active=False
#调查结束,显示结果
print("\n--- pill results ---")
for name,respose in respomses.items():
print(name+" would like to climb"+respose)
// 输出结果
--- pill results ---
erice would like to climbdenali
lynn would like to climbdevil's thumb