python基础之循环

while循环

message = ""
while message != 'quit':
	message = input("Enter 'quit' to end the program: ")
	if message != 'quit':
		print(message)

循环标志

active = True
while active:
	message = input("Enter 'quit' to end the program: ")
	if message != 'quit':
		print(message)
	else:
		active = False

break退出循环

while True:
	message = input("Enter 'quit' to end the program: ")
	if message != 'quit':
		print(message)
	else:
		break

countinue退出

current_number = 0
while current_number < 10:
	current_number += 1
	if current_number % 2 == 0:
		continue
	print(current_number)

while循环处理列表

unconfirmed_users = ['alice', 'brian', 'candace']
confirmed_users = []

while unconfirmed_users:
    current_user = unconfirmed_users.pop()
    
    print("Verifying user: " + current_user.title())
    confirmed_users.append(current_user)
    
print("\nThe following users have been confirmed:")
for confirmed_user in confirmed_users:
    print(confirmed_user.title())

删除多个特定元素

pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
print(pets)

while 'cat' in pets:
    pets.remove('cat')
    
print(pets)

posted @ 2020-10-03 20:00  Harry_666  阅读(165)  评论(0编辑  收藏  举报