whlie 和 for :根据条件执行某种操作。可以结合else使用
for 循环用于针对集合中每个元素的一个代码块,,而while不断运行,直到指定条件不满足为止。
note:死循环(常见bug,造成cup负载量更大,ctrl+c 关闭)
for循环中临时变量选择描述单个元素名称
>>> current_number = 1
>>> while current_number < 5:
... print(current_number)
... current_number += 1
...
1
2
3
4
1.选择合适的条件,让用户选择合适退出
2.使用标志:在要求很多条件都满足才继续运行的程序中,可定义一个变量,用于判断整个程序是否处于活动状态;
在复杂的程序中,任意一个事件都可能导致标志变成False,退出循环。
2-1比萨配料:编写一个循环,提示用户输入一系列的比萨配料,并在用户输入'quit'时结束循环。每当用户输入一种配料后,都打印一条消息,说我们会在比萨中添加这种配料。
# 2-1
>>> while True:
... topping = input(prompt)
... if topping != 'quit':
... print(" I'll add " + topping + " to your pizza.")
... else:
... break
...
What topping would you like on your pizza?
Enter 'quit' when you are finished: sala
I'll add sala to your pizza.
What topping would you like on your pizza?
Enter 'quit' when you are finished: quit
2-2 电影票:有家电影院根据观众的年龄收取不同的票价:不到 3 岁的观众免费;3~12 岁的观众为 10 美元;超过 12 岁的观众为 15 美元。请编写一个循环,在其中询问用户的年龄,并指出其票价
>>> prompt = "\n How old are you ?"
>>> prompt += "\nEnter '-1' when you are finished: "
>>> while True:
... age = int(input(prompt))
... if age == -1:
... break
... if age < 3:
... price = 0
... elif age < 12:
... price =10
... else:
... price =15
... print("You should pay " + str(price) +'.')
...
How old are you ?
Enter '-1' when you are finished: 9
You should pay 10.
How old are you ?
Enter '-1' when you are finished: -1
2-3 三个出口:以另一种方式完成练习 2-1或练习 2-2,在程序中采取如下所有做法。
在 while 循环中使用条件测试来结束循环。
使用变量 active 来控制循环结束的时机。
使用 break 语句在用户输入'quit'/-1时退出循环。
#2-1
>>> while active:
... topping = input(prompt)
... if topping != 'quit':
... print("I'll add " + topping + " to your pizza.")
... else:
... active =False
...
What topping would you like on your pizza?
Enter 'quit' when you are finished: sala
I'll add sala to your pizza.
What topping would you like on your pizza?
Enter 'quit' when you are finished: quit
#2-2
>>> prompt = "\n How old are you ?"
>>> prompt += "Enter '-1' when you finished"
>>> active = True
>>> while active:
... age = int(input(prompt))
... if age == -1 :
... active = False
... else:
... if age < 3:
... price = 0
... elif age < 12:
... price = 10
... else:
... price =15
... print("You should pay " + str(price) + " $.")
...
How old are you ?Enter '-1' when you finished0
You should pay 0 $.
How old are you ?Enter '-1' when you finished7
You should pay 10 $.
How old are you ?Enter '-1' when you finished-1
3.在python循环中都可以使用break语句,可以使用break语句来退出遍历列表或字典的for循环。
4.contiue:结束本次循环然后返回循环开头根据条件测试结果决定是否继续下一次循环,整个循环不一定结束。
>>> current_number = 0
>>> while current_number < 10:
... current_number += 1
... if current_number % 2 == 0:
... continue
... print(current_number)
...
1
3
5
7
9
>>>
5.break:跳出当前循环,不再运行循环中余下代码,也不管结果如何,可以让你控制程序流程,选择那些代码执行,那些不代码执行。
>>> prompt = "\n Please enter the your city"
>>> prompt += "\n(Enter 'quit ' when you are finished.)"
>>> while True:
... city = input(prompt)
... if city == 'quit':
... break
... else:
... print("I'd love to go to " + city.title() + "!")
...
Please enter the your city
(Enter 'quit ' when you are finished.)jack
I'd love to go to Jack!
Please enter the your city
(Enter 'quit ' when you are finished.)quit