zoukankan      html  css  js  c++  java
  • 出乎意料的else语句

    在python中你可能时不时不碰到else语句用在while和for循环中,请不要吃惊,先看看它的作用吧!
    实际上在循环语句中,else子句仅仅会在循环完毕后运行。即跳出循环的操作。如break,同一时候也会跳过
    else块。
    以下是一个来自python核心编程的样例

    def showMaxFactor(num):
        count = num/2
        while count > 1:
            if num%count == 0:
                print 'largest factor of %d is %d' % (num,count)
                break
            count -= 1
        else:
            print num, "is prime"
    
    for eachNum in range(10,21):
        showMaxFactor(eachNum)
  • 相关阅读:
    Luogu P3275 糖果
    Python基础学习
    SharePoint 2013
    Office
    KnockoutJS
    SharePoint 2013
    Bootstrap
    SharePoint 2013
    CSS
    AngularJS
  • 原文地址:https://www.cnblogs.com/gavanwanggw/p/7019356.html
Copyright © 2011-2022 走看看