Is there a better way to solve this?

Chris Rebert clp2 at rebertia.com
Sun May 22 02:42:00 EDT 2011


On Sat, May 21, 2011 at 11:28 PM, Ganapathy Subramanium
<sganapathy.subramanium at gmail.com> wrote:
> 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)

Never use the input() function in Python 2.x; it does an eval(), which
is evil. Always use raw_input() and an explicit conversion instead;
e.g.

Q = 'Enter the length till which you want to generate the fibonnaci series: \n'
try:
    length = int(raw_input(Q))
except ValueError:
    print 'Invalid input detected'
    exit(0)


Also, as a sidenote:

> if type(length) is int:

Is normally written:

if isinstance(length, int):

Cheers,
Chris
--
http://rebertia.com



More information about the Python-list mailing list