While循环是哟中利用条件语句,不断的执行某一段代码块,达到批量操作输出等一系列的操作,直到条件不满足或者被强制退出为止。

其工作流程如下: (图片来源菜鸟教程:http://www.runoob.com/python/python-while-loop.html  )

我们来看一个例子:

current_number = 10
while current_number <= 20:
print("Current number is : " + str(current_number))
current_number += 1 print("Final number: " + str(current_number)) '''
输出:
Current number is : 10
Current number is : 11
Current number is : 12
Current number is : 13
Current number is : 14
Current number is : 15
Current number is : 16
Current number is : 17
Current number is : 18
Current number is : 19
Current number is : 20
Final number: 21
'''

我们可以看到 变量current_number 的初始值为10, 在小于等于20的情况下,不断的被打印,被增长,直至增长到21的时候,跳出了循环。

我们也可以使用标志,比如True,False 等布尔值来进行循环:

flag = True
while flag:
number = int(input("Input a number less than 10:\n"))
if number < 10:
flag = True
else:
flag = False print("The game is end...") '''
输出:
Input a number less than 10:
2
Input a number less than 10:
11
The game is end...
Final number: 21
'''

使用 break 退出循环。在while循环中,你可以通过特定的条件语句,选择终止循环,不再继续运行余下的代码。

flag = True
while flag:
city = input("Input a city which you like: \n" + "Input 'Q' to quit...\n")
if city == 'Q':
break
else:
print("You like " + city) print("Thanks for your information.") '''
输出:
Input a city which you like:
Input 'Q' to quit...
ShangHai
You like ShangHai
Input a city which you like:
Input 'Q' to quit...
HangZhou
You like HangZhou
Input a city which you like:
Input 'Q' to quit...
Q
Thanks for your information.
'''

使用 continue 不再继续执行余下的代码块,返回到循环开头,继续下一轮循环。

number = 0
while number <= 10:
if number % 2 == 0:
print("The number is even: " + str(number))
number += 1
continue
print("Next...")
else:
number += 1 '''
输出:
The number is even: 0
The number is even: 2
The number is even: 4
The number is even: 6
The number is even: 8
The number is even: 10
'''

循环也可以和else 一起使用:

number = 0
while number <= 10:
if number % 2 == 0:
print("The number is even: " + str(number))
number += 1
continue
print("Next...")
else:
number += 1
else:
print("The number is equal or more than 10, stop loop.") '''
输出:
The number is even: 0
The number is even: 2
The number is even: 4
The number is even: 6
The number is even: 8
The number is even: 10
The number is equal or more than 10, stop loop.
'''

使用while 操作列表 List

peoples = ['Ralf', 'Clark', 'Leon']
while peoples:
people = peoples.pop()
print(people) '''
输出:
Leon
Clark
Ralf
'''
peoples = ['Ralf', 'Clark', 'Leon', 'Terry']
while 'Terry' in peoples:
peoples.remove('Terry')
print(peoples) '''
输出:
['Ralf', 'Clark', 'Leon']
'''

while循环语句可以解决程序中需要重复执行的操作。其循环执行的次数由循环条件确定,当循环条件满足时,重复执行某程序段,直到循环条件不成立为止。反复执行的程序段称为循环体,循环条件必须要在循环体中改变,否则可能会出现无限循环的结果

最新文章

  1. SignalR入门篇
  2. Code First :使用Entity. Framework编程(7) ----转发 收藏
  3. javascript的document中的动态添加标签
  4. ios - kvo观察者示例
  5. 转一个 Xcode 7 缺少 *.dylib库的解决方法
  6. linux下bom头导致的php调用php接口 返回的json字符串 无法转成 数组,即json字符串无法解码的问题
  7. Scalaz(31)- Free :自由数据结构-算式和算法的关注分离
  8. InstallShield 2010 使用 .net framework 4.5
  9. JS数组的基本用法
  10. 《BI项目笔记》多维数据集中度量值设计时的聚合函数
  11. [游戏模版4] Win32 显示鼠标位置
  12. angular $apply()以及$digest()讲解1
  13. Linux系统编程(15)——shell脚本语法
  14. 30+WordPress古典风格的主题-古典却不失时尚
  15. nginx笔记6-总结
  16. Android Studio教程11-RecycleView的使用
  17. crt证书iis 中引用 程序目录提示 System.UnauthorizedAccessException:拒绝访问
  18. JavaScript对象之深度克隆
  19. tessellate Architecture
  20. 如何使Ubuntu Linux12.04 LTS版可以用root用户登陆

热门文章

  1. redux知识点
  2. C# - 设计模式 - 策略模式
  3. codeblocks1712设置中文
  4. pythonのdjango
  5. Python爬虫从入门到进阶(1)之Python概述及爬虫入门
  6. 图文解说Win7系统机器上发布C#+ASP.NET网站
  7. VMware虚拟机从一台电脑复制到另一台电脑
  8. c/c++/java如何访问数据库(优秀博文)
  9. Selenium的webdriver的常用方法,鼠标事件
  10. 2018-2019-1 20189201 《LInux内核原理与分析》第六周作业