相干引荐:《python视频》
Python 编程中 while 语句用于轮回实行顺序,即在某前提下,轮回实行某段顺序,以处置惩罚须要反复处置惩罚的雷同使命。其基本形式为:
while 推断前提:
实行语句……
实行语句可所以单个语句或语句块。推断前提可所以任何表达式,任何非零、或非空(null)的值均为true。
当推断前提假false时,轮回完毕。
#!/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 = 1while i < 10: i += 1 if i%2 > 0: # 非双数时跳过输出 continue print i # 输出双数2、4、6、8、10 i = 1while 1: # 轮回前提为1一定建立 print i # 输出1~10 i += 1 if i > 10: # 当i大于10时跳出轮回 break
以上就是python限定轮回次数的要领的细致内容,更多请关注ki4网别的相干文章!