题目:

思考While循环,看看它的特点是什么?

知识点:

while循环

分析:

特点:while-loop(while 循环)。while-loop 会一直执行它下面的代码片段,直到它对应的布尔表达式为False 时才会停下来。

问题:while循环的条件总是为真时,该循环永不停止,直到天荒地老,海枯石烂。

解决:为了避免这样的问题,你需要遵循下面的规定:

1. 尽量少用while-loop,大部分时候for-loop 是更好的选择。
2. 重复检查你的while 语句,确定你测试的布尔表达式最终会变成False 。
3. 如果不确定,就在while-loop 的结尾打印出你要测试的值。看看它的变化。

代码分析:

i = 0
numbers = []
while i < 6:
print("At the top i is %d" % i)
numbers.append(i) i = i + 1
print("Numbers now: ", numbers)
print("At the bottom i is %d" % i) print("The numbers: ") for num in numbers:
print(num, end='\t')

结果:

At the top i is 0
Numbers now: [0]
At the bottom i is 1
At the top i is 1
Numbers now: [0, 1]
At the bottom i is 2
At the top i is 2
Numbers now: [0, 1, 2]
At the bottom i is 3
At the top i is 3
Numbers now: [0, 1, 2, 3]
At the bottom i is 4
At the top i is 4
Numbers now: [0, 1, 2, 3, 4]
At the bottom i is 5
At the top i is 5
Numbers now: [0, 1, 2, 3, 4, 5]
At the bottom i is 6
The numbers:
0 1 2 3 4 5 来自《笨办法学Python》

Python3练习题系列(03)的更多相关文章

  1. Python3练习题系列(09)——物以类聚,人以群分

    目标: 用类管理同类事物 解析: 用到“class”的编程语言被称作“Object Oriented Programming(面向对象编程)”语言.首先你需要做出“东西”来,然后你“告诉”这些东西去完 ...

  2. Python3练习题系列(10)——项目骨架构建

    目标: 如何创建<项目“骨架”目录> 包含:项目文件布局.自动化测试代码,模组,以及安装脚本. 由于编写一个Python文件可以作为一个模块,一个带__init__.py的目录算一个包. ...

  3. Python3练习题系列(06)——各种符号总结

    Python3中的各种符号总结 1关键字 import keyword print(keyword.kwlist, end='\t') ['False', 'None', 'True', 'and', ...

  4. Python3练习题系列(01)

    2018-06-13 题目: 根据用户回答做出相应的判断,完成一个“回答-判断”的小游戏 Python3知识点: if, else, elif 实例代码: print("You enter ...

  5. Python3练习题系列(07)——列表操作原理

    目标: 理解列表方法的真实含义. 操作: list_1.append(element) ==> append(list_1, element) mystuff.append('hello') 这 ...

  6. Python3练习题系列(08)——代码阅读方法及字典跳转表理解

    问题:分析下面代码 cities['_find'] = find_city city_found = cities['_find'](cities, state) 分析过程: 一个函数也可以作为一个变 ...

  7. Python3练习题系列(05)——设计和调试规则

    If 语句的常见规则 1. 每一个“if 语句”必须包含一个else: 2. 如果这个else 永远都不应该被执行到,因为它本身没有任何意义,那你必须在else 语句后面使用一个叫做die 的函数,让 ...

  8. Python3练习题系列(04)

    题目: 制作一个游戏 知识点: 函数.if_elif_else, while, exit 游戏图谱: 游戏代码: from sys import exit def gold_room(): print ...

  9. Python3练习题系列(02)

    题目: 思考循环结构,看看它是怎样运行的,对我们认识程序有何益处. 知识点: list, for-loop, range 练习代码: 练习1 the_count = [1, 2, 3, 4, 5] # ...

随机推荐

  1. 如何将SVN仓库转换为Git仓库

    按如下步骤操作就可以将SVN仓库完整的转换为Git仓库: 1) 将远程SVN仓库搬到本地(这一步主要是为了提高转换的速度,也可以忽略)     参考这篇文章: http://rongjih.blog. ...

  2. Linux mmc framework2:基本组件之block

    1.前言 本文主要block组件的主要流程,在介绍的过程中,将详细说明和block相关的流程,涉及到其它组件的详细流程再在相关文章中说明. 2.主要数据结构和API 2.1 struct mmc_ca ...

  3. python获取当前环境的编码

    # coding:gbk import sys import locale def p(f): print '%s.%s(): %s' % (f.__module__, f.__name__, f() ...

  4. openstack swift节点安装手册2-创建rings

    以下步骤需要在controller节点上进行操作: 切换到/etc/swift目录下进行如下操作: 一.创建account ring 1.创建account.builder文件 swift-ring- ...

  5. activit流程引擎启动流程报错

    代码如下: 目录结构 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); @Test public void ...

  6. 转载:第2章 Nginx的配置 概述《深入理解Nginx》(陶辉)

    原文:https://book.2cto.com/201304/19623.html Nginx拥有大量官方发布的模块和第三方模块,这些已有的模块可以帮助我们实现Web服务器上很多的功能.使用这些模块 ...

  7. hdu2586 lca倍增法

    倍增法加了边的权值,bfs的时候顺便把每个点深度求出来即可 #include<iostream> #include<cstring> #include<cstdio> ...

  8. 双倍浮向(双倍边距)(只有IE6出现)

    声明: web前端学习笔记,欢迎大神指点.联系QQ:1522025433. 描述:在IE6中,一个居左(或居右)浮动的元素放置进一个容器盒(box),并在浮动元素上使用了左边距(或右边距) 在ie6内 ...

  9. JSON.NET 空值处理, 数字转字符,时间格式化

    public static string ToJsonString(this Object obj) { IsoDateTimeConverter idtc = new IsoDateTimeConv ...

  10. 用SQL统计每分钟的访问量

    以前面试没有理解到它什么意思的一道题,回忆中是这个题意 ), to_char(r.datelastmaint, 'yyyy-mm-dd hh24:mi'), sum(abs(r.tranamt)) f ...