Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #5962 > unrolled thread

Re: Is there a better way to solve this?

Started byChris Rebert <clp2@rebertia.com>
First post2011-05-21 23:42 -0700
Last post2011-05-21 23:42 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Is there a better way to solve this? Chris Rebert <clp2@rebertia.com> - 2011-05-21 23:42 -0700

#5962 — Re: Is there a better way to solve this?

FromChris Rebert <clp2@rebertia.com>
Date2011-05-21 23:42 -0700
SubjectRe: Is there a better way to solve this?
Message-ID<mailman.1906.1306046523.9059.python-list@python.org>
On Sat, May 21, 2011 at 11:28 PM, Ganapathy Subramanium
<sganapathy.subramanium@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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web