while循环简介
- for循环用于针对集合中的每个元素的一个代码块,
- while循环是不断的运行,直到指定的条件不满足为止。
使用while循环
current_number = 1
while current_number <= 5:
print(current_number)
current_number += 1
1
2
3
4
5
让用户何时选择退出
- 定义一个退出值,只要用户输入发热不是这个值,程序就一直运行。
prompt = ("\nTell me something,and I will repeart it back to you: ")
prompt += ("\nEnter 'quit' to end the program." )
message = ''
while message != 'quit':
message = input(prompt)
if message != 'quit':
print(message)
Tell me something,and I will repeart it back to you:
Enter 'quit' to end the program.aa
aa
Tell me something,and I will repeart it back to you:
Enter 'quit' to end the program.quit
***Repl Closed***
使用标志
- 要求很多条件都满足才继续运行的程序,可定义一个变量,用于判断一个程序是否属于一个活动状态,
- 这个变量被称之为标志。
prompt = ("\nTell me something,and I will repeart it back to you: ")
prompt += ("\nEnter 'quit' to end the program." )
active = True
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print(message)
Tell me something,and I will repeart it back to you:
Enter 'quit' to end the program.aa
aa
Tell me something,and I will repeart it back to you:
Enter 'quit' to end the program.quit
***Repl Closed***
- 标识很有用,在任何事件导致活动标记为False时,while循环将会退出。
使用break退出循环
- break语句用于控制程序流程。
- 使用break语句,立即退出while循环,不在运行循环中余下的代码。
prompt = '\nPlease enter the name of a city you have citiies:'
prompt += "\n(Enter 'quit' when you are finished.)"
while True:
city = input(prompt)
if city == 'quit':
break
Please enter the name of a city you have citiies:
(Enter 'quit' when you are finished.)beijing
Please enter the name of a city you have citiies:
(Enter 'quit' when you are finished.)quit
***Repl Closed***
在while循环中使用continue
- 使用continue语句,返回到循环开头,并根据循环条件测试结果是否继续执行循环。
current_number = 0
while current_number <= 10:
current_number += 1
if current_number % 2 == 0:
continue
print(current_number)
1
3
5
7
9
11
避免无限循环
- 要避免编写无限循环,务必对每个while循环进行测试,确保他按照预期那样结束。
使用while循环来处理列表和字典
- for循环是一种遍历列表的有效方式,for循环中不应修改列表,否则将导致python难以追踪其中元素。
- 要遍历列表并对其进行修改,使用while循环。
unconfirmed_user = ['alice','brian','candace']
confirmed_user = []
while unconfirmed_user:
current_user = unconfirmed_user.pop()
print("Verifying user: " + current_user.title())
confirmed_user.append(current_user)
Verifying user: Candace
Verifying user: Brian
Verifying user: Alice
[Finished in 0.4s]
删除包含特定值的所有列表元素
pets = ['dog','cat','dog','goldfish','cat','rabbit','cat']
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
['dog', 'dog', 'goldfish', 'rabbit']
使用用户输入来填充字典
responses = {}
polling_active = True
while polling_active:
name = input("\nWhat is your name: ")
respone = input("\nwhich mountain would you like to climb someday? ")
responses[name] = respone
repeat = input("\nWould you like to let another person respond?(yes/no)")
if repeat == 'no':
polling_active = False
print("\n----POLL Results----")
for name, respone in responses.items():
print(name + " wonld like to climb " + respone + ".")
What is your name: alice
which mountain would you like to climb someday? aa
Would you like to let another person respond?(yes/no)yes
What is your name: nana
which mountain would you like to climb someday? bb
Would you like to let another person respond?(yes/no)no
----POLL Results----
alice wonld like to climb aa.
nana wonld like to climb bb.
***Repl Closed***
总结
- 使用input()来获取用户的输入信息。
- 如何处理文本和数字输入。
- 如何使用while循环安装用户的要求不断运行。
- 多种控制while循环的方式。
- 设置获取标志