Python学习笔记[5]---else语句和with语句

 

---else--

在Python语句中else比在其他的一些语言更加灵活,else不仅可以和if语句进行组合使用,还可以和循环控制语句一起使用,有一点需要注意的就是:在和循环语句一起使用的时候,else只有在循环完成后才被调用,也就是说,如果是循环被break而结束的话,else后面的语句是不会执行的。

example:

#!/usr/bin/Python
#encoding=''utf-8

def ShowMaxFactor(num):
    if not isinstance(num,int):
        print('pleaes input a integer')

    else:
        count = num // 2
        while count > 1:
            if num % count == 0:
                print('the max factor is {0}'.format(count))
                break
            count -= 1
        else:
            print('{0} is a prime number',num)


if __name__ == '__main__':
    ShowMaxFactor(123)

运行结果

>>> ================================ RESTART ================================
>>> 
the max factor is 41

 

---with----

 

default:null

 

posted on 2015-01-29 13:38  joker_02  阅读(316)  评论(0编辑  收藏  举报

导航