Is there a better way to solve this?

Ganapathy Subramanium sganapathy.subramanium at gmail.com
Sun May 22 02:28:51 EDT 2011


Hello,

I'm a new bie to python programming and on the processing of learning python
programming. I have coded my first program of fibonnaci generation and would
like to know if there are better ways of achieving the same.

I still feel quite a few things to be improved. Just wanted experts thoughts
on this.

try:

    length = input('Enter the length till which you want to generate the
fibonnaci series: \n')
    print type(length)

except:
    print 'Invalid input detected'
    exit(0)

if type(length) is int:
    result = []
    a = 0
    b = 1
    result.append(a)
    result.append(b)
    if length > 2:
        count = 2
        while count  < length:
            c = a + b
            a = b
            b = c
            result.append(c)
            count += 1
        else:
            print result
            print 'The lenght of result is : ' , len(result)
    else:
        print 'Fibonacci series could not be generated !!!'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110522/5c452665/attachment.html>


More information about the Python-list mailing list