循环

本文介绍了Python中的循环控制结构,包括while和for循环的使用。for循环适用于遍历集合,while循环则持续执行直到指定条件不满足。强调了避免死循环的重要性,并提供了三个示例:比萨配料输入、电影票价格计算以及使用while循环实现类似功能。同时讲解了break和continue语句的用途,帮助控制循环流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值