Core python

def fabonacci2():
    '''
        test fabonacci sequence 's max number & write into a file,but fails,cause it always run,finally statement not performance 
    '''
    a,b=1,1
    try:
        file=open('fabonacci_sequence.txt','wb')
        while 1:
    #        print a,
            file.write(str(a))
            file.write(',\n\r')
            a,b=b,a+b
            file.flush()#这里关键没有这句,文件中没有数据,不过后面的finally我一直没等到。我最大的一个数是好多位。
    finally:
        file.close()
        print 'close'
def fabonacci():
    '''
        attention yield,it only used in function ,not in class(personal).here yield is stopped,til next() run
    '''
    a,b=0,1
    while 1:
        yield b
        a,b=b,a+b
if __name__ == '__main__':
    fb=fabonacci()
#    print fb.next()
    i=1
    num=10000
    while i<num:
        i=i+1
        fb.next()
        if i==num :
            print 'Fabonacci sequence No.',num,' number is ',fb.next()
    fabonacci2()

你可能感兴趣的:(Core python)