python的while如何使用-Python教程

资源魔 34 0
Python While 轮回语句

Python编程中while语句用于轮回执行顺序,即正在某前提下,轮回执行某段顺序,以解决需求反复解决的相反义务。

其根本方式为:(保举学习:Python视频教程)

while 判别前提:
    执行语句……

执行语句能够是单个语句或语句块。判别前提能够是任何表白式,任何非零、或非空(null)的值均为true。

当判别前提假false时,轮回完结。

while 语句时另有另外两个首要的饬令 continue,break 来跳过轮回,continue 用于跳过该次轮回,break 则是用于加入轮回,别的"判别前提"还能够是个常值,示意轮回必然成立,详细用法以下:

# continue 以及 break 用法
 i = 1while i < 10:   
    i += 1
    if i%2 > 0:     # 非双数时跳过输入
        continue
    print i         # 输入双数二、四、六、八、10
 i = 1while 1:            # 轮回前提为1必然成立
    print i         # 输入1~10
    i += 1
    if i > 10:     # 当i年夜于10时跳出轮回
        break

轮回应用 else 语句

正在 python 中,while … else 正在轮回前提为 false 时执行 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

更多Python相干技巧文章,请拜访Python教程栏目进行学习!

以上就是python的while若何应用的具体内容,更多请存眷资源魔其它相干文章!

标签: Python python教程 python编程 python使用问题

抱歉,评论功能暂时关闭!