Python While 循环语句

Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为:

while 判断条件:
执行语句……

执行语句可以是单个语句或语句块。判断条件可以是任何表达式,任何非零、或非空(null)的值均为true。

当判断条件假false时,循环结束。

执行流程图如下:

while 语句时还有另外两个重要的命令 continue,break 来跳过循环,continue 用于跳过该次循环,break 则是用于退出循环,此外"判断条件"还可以是个常值,表示循环必定成立,具体用法如下:

 #coding=utf-8
'''
@author:Nelson.huang
@Date : 2017.07,25
@Funtion: while循环的练习:
用户输入一个小于1000的数字,输出所有结果,并停在用户输入的结果这里,并且询问用户是否继续循环。
如果用户输入非‘N’的字符串,继续循环,并再次要求用户输入一个停下来的数字,继续。 ''' print_num = input('Please input the count :')
count = 0
while count < 1000:
if count == print_num:
print "There you got the number:", count
choice = raw_input('Do you want to continue to loop?(y/n):')
if choice == 'n':
break
else:
print_num = input('Please re-input the count:') if print_num < count:
print 'your num is passed, please re-input:'
print_num = input('Please input the count :')
continue else:
print 'loop:', count count +=1
执行结果:

C:\Python27\python.exe C:/Work/python/while.py
Please input the count :5
loop: 0
loop: 1
loop: 2
loop: 3
loop: 4
There you got the number: 5
Do you want to continue to loop?(y/n):y
Please re-input the count:8
loop: 5
loop: 6
loop: 7
There you got the number: 8
Do you want to continue to loop?(y/n):y
Please re-input the count:4
your num is passed, please re-input:
Please input the count :16
loop: 8
loop: 9
loop: 10
loop: 11
loop: 12
loop: 13
loop: 14
loop: 15
There you got the number: 16
Do you want to continue to loop?(y/n):


最新文章

  1. Java基础知识梳理《一》
  2. Centos7 修改SSH 端口
  3. poj1733(种类并查集+离散化)
  4. 基于Microchip单片机的触摸感应技术
  5. 了解OpenStack
  6. 【转】 IOS 项目配置--构建输出DIR
  7. php二叉树算法
  8. nginx和tomcat的响应时间
  9. linux新建磁盘并分区
  10. 丑数 LeeTCode
  11. 设置Eclipse的workspace路径
  12. 使用kafka consumer api时,中文乱码问题
  13. 20155211 2016-2017-2 《Java程序设计》第六周学习总结
  14. did not call through to super.onCreate()
  15. idea linux 启动权限不足的问题
  16. FlexBox弹性盒布局
  17. 服务器编程入门(13) Linux套接字设置超时的三种方法
  18. 【CodeForces】915 E. Physical Education Lessons 线段树
  19. bootstrap-table组合表头
  20. Converter -&gt; public static int ToInt32(double value) 你用对了么?

热门文章

  1. 磁盘文件I/O,SSD结构,局部性原理 笔记
  2. 构建工具Gulp
  3. MyEclipse解决SVN同步冲突问题conflict in the working copy obstructs the current operation
  4. 在WIN SERVER 2016上安装DOCKER(带过坑)
  5. angular指令ng-class巧用
  6. JS学习笔记——JavaScript继承的6种方法(原型链、借用构造函数、组合、原型式、寄生式、寄生组合式)
  7. mac sourcetree启用外部合并工具(beyond compare)
  8. Discuz开发帮助
  9. react-native 学习 ----- React Navigation
  10. 【转载】QT QTableView用法小结