Python While循环语句实例演示及原理解析


Posted in Python onJanuary 03, 2020

这篇文章主要介绍了Python While循环语句实例演示及原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

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

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

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

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

执行流程图如下:

Python While循环语句实例演示及原理解析

Python while 语句执行过程

Python While循环语句实例演示及原理解析

实例:

#!/usr/bin/python
 count = 0
 while (count < 9):  print 'The count is:', count  count = count + 1 print "Good bye!"

以上代码执行输出结果:

The count is: 0
The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!

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

# continue 和 break 用法
i = 1
while i < 10:    i += 1   if i%2 > 0:   # 非双数时跳过输出
    continue
  print i     # 输出双数2、4、6、8、10

i = 1
while 1:      # 循环条件为1必定成立
  print i     # 输出1~10
  i += 1
  if i > 10:   # 当i大于10时跳出循环
    break

无限循环

如果条件判断语句永远为 true,循环将会无限的执行下去,如下实例:

#coding=utf-8
#!/usr/bin/python

var = 1
while var == 1 : # 该条件永远为true,循环将无限执行下去
  num = raw_input("Enter a number :")
  print "You entered: ", num

print "Good bye!"

以上实例输出结果:

Enter a number :20
You entered: 20
Enter a number :29
You entered: 29
Enter a number :3
You entered: 3
Enter a number between :Traceback (most recent call last):
 File "test.py", line 5, in <module>
  num = raw_input("Enter a number :")
KeyboardInterrupt

注意:以上的无限循环你可以使用 CTRL+C 来中断循环。

循环使用 else 语句

在 python 中,for … else 表示这样的意思,for 中的语句和普通的没有区别,else 中的语句会在循环正常执行完(即 for 不是通过 break 跳出而中断的)的情况下执行,while … else 也是一样。

#!/usr/bin/python

count = 0
while count < 5:
  print count, " is less than 5"
  count = count + 1
else:
  print count, " is not less than 5"

以上实例输出结果为:

0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5

简单语句组

类似if语句的语法,如果你的while循环体中只有一条语句,你可以将该语句与while写在同一行中, 如下所示:

#!/usr/bin/python
flag = 1
while (flag): print 'Given flag is really true!'
print "Good bye!"

注意:以上的无限循环你可以使用 CTRL+C 来中断循环。

While循环语句实例

猜拳小游戏

#!/usr/bin/python
# -*- coding: UTF-8 -*-

import random
while 1:
  s = int(random.randint(1, 3))
  if s == 1:
    ind = "石头"
  elif s == 2:
    ind = "剪子"
  elif s == 3:
    ind = "布"
  m = raw_input('输入 石头、剪子、布,输入"end"结束游戏:')
  blist = ['石头', "剪子", "布"]
  if (m not in blist) and (m != 'end'):
    print "输入错误,请重新输入!"
  elif (m not in blist) and (m == 'end'):
    print "\n游戏退出中..."
    break
  elif m == ind :
    print "电脑出了: " + ind + ",平局!"
  elif (m == '石头' and ind =='剪子') or (m == '剪子' and ind =='布') or (m == '布' and ind =='石头'):
    print "电脑出了: " + ind +",你赢了!"
  elif (m == '石头' and ind =='布') or (m == '剪子' and ind =='石头') or (m == '布' and ind =='剪子'):
    print "电脑出了: " + ind +",你输了!"

以上实例输出结果为:

输入 石头、剪子、布,输入"end"结束游戏:石头
 电脑出了: 石头,平局!
 输入 石头、剪子、布,输入"end"结束游戏:石头  
 电脑出了: 剪子,你赢了!
 输入 石头、剪子、布,输入"end"结束游戏:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中使用urllib2防止302跳转的代码例子
Jul 07 Python
浅谈python抛出异常、自定义异常, 传递异常
Jun 20 Python
详解python基础之while循环及if判断
Aug 24 Python
对python字典过滤条件的实例详解
Jan 22 Python
python3实现小球转动抽奖小游戏
Apr 15 Python
Apache部署Django项目图文详解
Jul 30 Python
python实现输出一个序列的所有子序列示例
Nov 18 Python
Python列表操作方法详解
Feb 09 Python
python pandas移动窗口函数rolling的用法
Feb 29 Python
Python IDE环境之 新版Pycharm安装详细教程
Mar 05 Python
Python ini文件常用操作方法解析
Apr 26 Python
python图像处理 PIL Image操作实例
Apr 09 Python
在Pytorch中计算卷积方法的区别详解(conv2d的区别)
Jan 03 #Python
Python综合应用名片管理系统案例详解
Jan 03 #Python
Python tkinter常用操作代码实例
Jan 03 #Python
PyTorch中的padding(边缘填充)操作方式
Jan 03 #Python
nginx搭建基于python的web环境的实现步骤
Jan 03 #Python
Python如何使用字符打印照片
Jan 03 #Python
Pytorch.nn.conv2d 过程验证方式(单,多通道卷积过程)
Jan 03 #Python
You might like
Zend公司全球首推PHP认证
2006/10/09 PHP
dedecms 制作模板中使用的全局标记图文教程
2007/03/11 PHP
PHP 数组和字符串互相转换实现方法
2013/03/26 PHP
PHP上传文件时自动分配路径的方法
2015/01/09 PHP
摘自织梦CMS中的图片处理类
2015/08/08 PHP
js下关于onmouseout、事件冒泡的问题经验小结
2010/12/09 Javascript
jQuery中before()方法用法实例
2014/12/25 Javascript
jquery图片滚动放大代码分享(1)
2015/08/25 Javascript
JS中改变this指向的方法(call和apply、bind)
2016/03/26 Javascript
JS JSOP跨域请求实例详解
2016/07/04 Javascript
使用原生js写ajax实例(推荐)
2017/05/31 Javascript
angularjs2中父子组件的数据传递的实例代码
2017/07/05 Javascript
form表单序列化详解(推荐)
2017/08/15 Javascript
js实现搜索栏效果
2018/11/16 Javascript
一些你可能不熟悉的JS知识点总结
2019/03/15 Javascript
微信小程序中显示倒计时代码实例
2019/05/09 Javascript
详解vue 在移动端体验上的优化解决方案
2019/05/20 Javascript
jQuery/JS监听input输入框值变化实例
2019/10/17 jQuery
node读写Excel操作实例分析
2019/11/06 Javascript
微信小程序开发搜索功能实现(前端+后端+数据库)
2020/03/04 Javascript
解决ant-design-vue中menu菜单无法默认展开的问题
2020/10/31 Javascript
flask中主动抛出异常及统一异常处理代码示例
2018/01/18 Python
Python数据分析之获取双色球历史信息的方法示例
2018/02/03 Python
pandas 快速处理 date_time 日期格式方法
2018/11/12 Python
美国椅子和沙发制造商:La-Z-Boy
2020/10/25 全球购物
面料业务员岗位职责
2013/12/26 职场文书
综治宣传月活动总结
2014/04/28 职场文书
给校长的建议书400字
2014/05/15 职场文书
学校校庆演讲稿
2014/05/22 职场文书
分公司经理任命书
2014/06/05 职场文书
车间安全生产标语
2014/06/06 职场文书
民主生活会批评与自我批评总结
2014/10/17 职场文书
云台山导游词
2015/02/03 职场文书
民事诉讼答辩状范文
2015/05/21 职场文书
入党转正申请自我鉴定
2019/06/25 职场文书
python实现简易名片管理系统
2021/04/11 Python