天天看點

python基本循環_python基礎之循環

while循環

message = ""

while message != 'quit':

message = input("Enter 'quit' to end the program: ")

if message != 'quit':

print(message)

python基本循環_python基礎之循環

循環标志

active = True

while active:

message = input("Enter 'quit' to end the program: ")

if message != 'quit':

print(message)

else:

active = False

python基本循環_python基礎之循環

break退出循環

while True:

message = input("Enter 'quit' to end the program: ")

if message != 'quit':

print(message)

else:

break

python基本循環_python基礎之循環

countinue退出

current_number = 0

while current_number < 10:

current_number += 1

if current_number % 2 == 0:

continue

print(current_number)

python基本循環_python基礎之循環

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())

python基本循環_python基礎之循環

删除多個特定元素

pets = ['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']

print(pets)

while 'cat' in pets:

pets.remove('cat')

print(pets)

python基本循環_python基礎之循環