while循环语句及练习题

Python 编程中 while 语句用于循环执行程序,即在某条件下,循环执行某段程序,以处理需要重复处理的相同任务。其基本形式为: while 判断条件: 执行语句...... 执行语句可以是单个语句或语句块。判断条件可以是任何表达式,任何非零、或非空(null)的值均为true。
当判断条件假 false 时,循环结束。

实例:

  1. count = 0
  2. while (count < 9):
  3. print ('The count is:', count)
  4. count = count + 1
  5. print("Good bye!")

运行结果:

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

continue 和 break 用法

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

  1. i = 1
  2. while i < 10:
  3. i += 1
  4. if i%2 > 0: # 非双数时跳过输出
  5. continue
  6. print(i) # 输出双数2、4、6、8、10

运行结果:

  1. 2
  2. 4
  3. 6
  4. 8
  5. 10
  1. i = 1
  2. while 1: # 循环条件为1必定成立
  3. print(i) # 输出1~10
  4. i += 1
  5. if i > 10: # 当i大于10时跳出循环
  6. break

运行结果:

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10

无限循环

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

  1. var = 1
  2. while var == 1 : # 该条件永远为true,循环将无限执行下去
  3. num = input("Enter a number :")
  4. print ("You entered: ", num)
  5. print ("Good bye!")
  1. Enter a number :3
  2. You entered: 3
  3. Enter a number :4
  4. You entered: 4
  5. Enter a number :5
  6. You entered: 5
  7. Enter a number :6
  8. You entered: 6
  9. Enter a number :7
  10. You entered: 7
  11. Enter a number :8
  12. You entered: 8
  13. Enter a number :100
  14. You entered: 100
  15. Enter a number :Traceback (most recent call last):
  16. File "c:/1.py", line 76, in <module>
  17. # num = input("Enter a number :")
  18. KeyboardInterrupt

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

循环使用 else 语句

在 python 中,while … else 在循环条件为 false 时执行 else 语句块:

  1. count = 0
  2. while count < 5:
  3. print (count, " is less than 5")
  4. count = count + 1
  5. else:
  6. print (count, " is not less than 5")

运行结果:

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

相关练习题

1.输出1,2,3,4,5,6,8,9,10
  1. count = 1
  2. while count <= 10:
  3.   if count == 7:
  4.     pass
  5.   else:
  6.     print(count)
  7.   count = count + 1
2.输出1-100所有数的和
  1. n = 1
  2. m = 0
  3. while n < 101:
  4.   m = m + n
  5.   n = n + 1
  6. print(m)
3.输出100以内所有奇数
  1. n = 1
  2. while n < 101:
  3.   temp = n % 2
  4.   if temp == 0:
  5.     print(n)
  6.   else:
  7.     pass
  8.   n = n +1
4.输出1-2+3-4+5.....99所有数的和
  1. n = 1
  2. m = 0
  3. while n < 100:
  4.   temp = n % 2
  5.   if temp == 0:
  6.     m = m - n
  7.   else:
  8.     m = m + n
  9.   n = n + 1
  10. print(m)

参考资料:

1.Python While 循环语句 | 菜鸟教程:https://www.runoob.com/python/python-while-loop.html

python:while循环语句及练习题的更多相关文章

  1. Python for 循环语句

    Python for 循环语句 Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串. 语法: for循环的语法格式如下: for iterating_var in sequenc ...

  2. python的循环语句

    python的循环语句有两种:for 和 while,for循环是对可迭代对象进行迭代并处理,因此for的对象是一个可以迭代的对象,而while循环的条件则是一个布尔值可以是一个返回布尔值的表达式. ...

  3. Python for循环语句

    Python for 循环语句:遍历任何序列的项目,可以是字符串.列表.元组.字典.集合对象. 流程图: 第一种: ''' for 迭代对象 in 序列: 代码块(一行语句或多行代码) ''' 第二种 ...

  4. python基础-循环语句(5)

    一.循环语句介绍 一般情况下,需要多次重复执行的代码,都可以用循环的方式来完成 循环不是必须要使用的,但是为了提高代码的重复使用率,所以有经验的开发者都会采用循环 二.常见的循环形式 while循环 ...

  5. python 之 循环语句

    python提供了for循环和while循环以及嵌套循环(在python中没有do..while循环) while 循环语法: while 判断条件: 执行语句...... 实际案例: numbers ...

  6. Python学习-7.Python的循环语句-for语句

    Python中循环可以使用for语句来实现 list = ['Tom','Lucy','Mary'] for name in list: print(name) 则将会依次输出Tom Lucy Mar ...

  7. Python从零开始——循环语句

    一:Python循环语句知识概览 二:while循环 三:for遍历 四:循环控制

  8. Python(循环语句与数据类型)

    循环语句 对于python来说 基本上循环用的两个 wile 跟静态语言相似 下来是for循环 这个就跟静态语言大大不同了 wile 条件:–>while 循环也就是 当条件为真的时候会一直循环 ...

  9. python基础-循环语句while

    循环语句:while\for\嵌套 循环控制语句:break\continue break:跳出整个循环,不会再继续循环下去 continue:跳出本次循环,继续下一次循环 while循环: coun ...

随机推荐

  1. python去除字符串中间空格的方法

    1.使用字符串函数replace a = 'hello world' a.replace(' ', '') # 'helloworld' 2.使用字符串函数split a = ''.join(a.sp ...

  2. 2018-10-2-win10-uwp-win2d-特效

    title author date CreateTime categories win10 uwp win2d 特效 lindexi 2018-10-02 21:20:46 +0800 2018-6- ...

  3. Sql 使用游标

    DECLARE data_cursor CURSOR FOR WITH T0 AS ( SELECT COUNT(f.DeptID) SubmitCount , f.DeptID FROM biz.F ...

  4. OpenVINO 安装及使用

    安装 https://docs.openvinotoolkit.org/latest/_docs_install_guides_installing_openvino_linux.html 使用 文档 ...

  5. 一、ARM

    1.1 ARM 分类 1.1.1 版本号分类 以前分类的是 ARM7,ARM9... ARM11,在 ARM11 之后,就是以 Cortex 系列分类了: Cortex-R:应用在实时系统上的系列 C ...

  6. Flutter-stack層疊樣式

    alignment調整佈局 var stack = new Stack( alignment: Alignment.center,//元素居中 //alignment: Alignment (1,1) ...

  7. 动态全屏弹窗特效 Morphing Modal Window

    动态变形弹窗特效 Morphing Modal Window 弹出窗体是网页常用的一个交互设计,在这个注重交互动画体验的时代,网页弹窗也可以来点新鲜的点子,比如今天分享的CSS 变形Modal Win ...

  8. Linux下git安装配置

    一.Linux下git安装配置 2013-07-28 20:32:10|  分类: 默认分类 |  标签:linux  git  server  |举报|字号 订阅     http://abomby ...

  9. ht-7 treeSet特性

    TreeSetTreeSet可以对set集合中的元素进行排序,默认按照asic码表的自然顺序排序,之所以treeset能排序是因为底层是二叉树,数据越多越慢,TreeSet是依靠TreeMap来实现的 ...

  10. LNMP环境搭建最好用的两种方法(亲测)

    经历了一个PHP服务器项目,手动编译部署PHP,Swoole环境太让人郁闷了,所以尝试过两种不错的方法,分享出来方便同样经历痛苦的coder. 第一种方式: 安装LNMP按照这里的步骤执行,网址戳我 ...