#/usr/bin/python 
#coding=utf-8
#@Time :2017/10/18 15:31
#@Auther :liuzhenchuan
#@File :while 循环.py 示例1:
 n = 0
while True:
print 'hello world'
if n == 10:
break
n +=1
打印如下:
 hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world
hello world

示例2:

 n = 0
while True:
if n==10:
break
print n ,'hello'
n +=1

打印如下:

 0 hello
1 hello
2 hello
3 hello
4 hello
5 hello
6 hello
7 hello
8 hello
9 hello

示例3:

#条件为假的时候,也是退出循环.输入空字符的时候也退出循环
 rinput = ''
while rinput !='q':
rinput = raw_input('please input something,q is quite: ')
print 'hello'
if rinput == '':
break

示例4:

#应用逻辑非判断,退出while循环。while条件为假时,退出while循环
 rinput = ''
while rinput !='q':
rinput = raw_input('please input something,q is quite: ')
print 'hello'
if not rinput :
break

示例5:

# while...else...:正常结束while循环时会打印else后面的语句.与for...else...循环体一样
 rinput = ''
while rinput !='q':
rinput = raw_input('please input something,q is quite: ')
print 'hello'
if not rinput :
break
else:
print 'hello world'

示例6:

#while 循环嵌套if循环体,支持contine
 rinput = ''
while rinput !='q':
rinput = raw_input('please input something,q is quite: ')
print 'hello'
if not rinput :
break
elif rinput == 'quite':
continue
print 'continue'
else:
print 'hello world'


												

python 基础 2.4 while 循环的更多相关文章

  1. 『Python基础-7』for循环 & while循环

    『Python基础-7』for循环 & while循环 目录: 循环语句 for循环 while循环 循环的控制语句: break,continue,pass for...else 和 whi ...

  2. Python基础(2)——循环和分支[xiaoshun]

    一.瞎扯 世界上一切的系统都可以被'分支'表示.循环也是分支,只不过又重复之前的'分支'选择罢了.程序如人生,每一次的'分支',每一次的选择,都会有不同的结果: 有的选择止步不前,无限循环: 有的选择 ...

  3. Python基础数据类型与for循环

    数据类型:int,bool,str,list, tuple元组,dict字典. 1.数字:12,3,4 在使用print打印数字时,在终端界面中无法判断出打印的是什么类型,当我们需要知道一个值是什么类 ...

  4. Python基础之条件和循环

    阅读目录 一.if语句 1.1功能 1.2语法 1.2.1:单分支,单重条件判断 1.2.2:单分支,多重条件判断 1.2.3:if + else 1.2.4:多分支if + elif +else 1 ...

  5. Python基础7- 流程控制之循环

    循环: 把一段代码重复性的执行N次,直到满足某个条件为止. 为了在合适的时候,停止重复执行,需要让程序出现满足停止循环的条件.Python中有三种循环(实质只有两种): while循环 for循环 嵌 ...

  6. Python基础(条件判断和循环) if elif else for while break continue;

    条件判断 计算机之所以能做很多自动化的任务,因为它可以自己做条件判断. 比如,输入用户年龄,根据年龄打印不同的内容,在Python程序中,用if语句实现: age = 20 if age >= ...

  7. Python之路,Day2 - Python基础,列表,循环

    1.列表练习name0 = 'wuchao'name1 = 'jinxin'name2 = 'xiaohu'name3 = 'sanpang'name4 = 'ligang' names = &quo ...

  8. Python基础 条件判断和循环

    pyhton if 语句 if 语句后接表达式,然后用: 表示代码块. age = 20 if age >= 18: print 'your age is', age print 'adult' ...

  9. Python基础、判断、循环、列表、字典,day1

    一.Python 简介 1.介绍 Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标 ...

  10. 【Python基础】之for循环、数组字典

    一. for循环实例 1.循环字符串 Python Shell: for i in "hello": print(i) h e l l o 2.循环数组Python Shell: ...

随机推荐

  1. hdu 2654(欧拉函数)

    Become A Hero Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  2. Springboot如何优雅的解决ajax+自定义headers的跨域请求

    1.什么是跨域 由于浏览器同源策略(同源策略,它是由Netscape提出的一个著名的安全策略.现在所有支持JavaScript 的浏览器都会使用这个策略.所谓同源是指,域名,协议,端口相同.),凡是发 ...

  3. Data structure basics - Java Implementation

    Stack & Queue Implementations FixedCapacityQueue package cn.edu.tsinghua.stat.mid_term; import j ...

  4. 【Objective-C Runtime动态加载】---动态创建类Class

    a.使用objc_allocateClassPair创建一个类Class    const char * className = "Calculator";    Class kc ...

  5. Rebound动画框架简单介绍

    Rebound动画框架简单介绍 Android菜鸟一枚,有不对的地方希望大家指出,谢谢. 最近在接手了一个老项目,发现里面动画框架用的是facebook中的Rebound框架,由于以前没听说过,放假时 ...

  6. C#反射获取属性值和设置属性值

    /// /// 获取类中的属性值 /// public string GetModelValue(string FieldName, object obj) { try { Type Ts = obj ...

  7. 各类免费的API接口分享,无限次

    各类免费的API接口分享: 手机号码归属地API:https://www.juhe.cn/docs/api/id/11 历史上的今天API:https://www.juhe.cn/docs/api/i ...

  8. webstrom 代码工具(转http://www.cnblogs.com/tangdanni11/p/5149063.html)

    Emmet的前身是大名鼎鼎的Zen coding,如果你从事Web前端开发的话,对该插件一定不会陌生.它使用仿CSS选择器的语法来生成代码,大大提高了HTML/CSS代码编写的速度,比如下面的演示: ...

  9. 【Excle数据透视】如何用含有单元格的数据来创建数据透视

    取消合并单元格,填充相同内容项,然后创建数据透视表. 如下图:需要使用数据创建数据透视表 步骤一 开始→格式刷,然后对单元格区域G2:G15使用格式刷功能,保留合并单元格格式 步骤二 选中A2:A18 ...

  10. 【DB2】DB2使用IMPORT命令导入含有自增长列的表报错处理

    1.启动数据库:db2start 2.创建数据库:create db TestDB using codeset gbk territory CN  collate using identity 3.连 ...