[sf-lug] ThinkPython: coding problem

Alex Kleider a_kleider at yahoo.com
Sat Oct 11 18:36:59 PDT 2008


--- On Sat, 10/11/08, Jeff Bragg <jackofnotrades at gmail.com> wrote:

> It appears to me that it crashed due to a type clash. 

undoubtedly!

 I
> suspect Asheesh was
> referring to user-defined exceptions to catch this type of
> clash *before*
> trying to convert a string to an integer (by
> sanity-checking the type of the
> input value, for instance), thus allowing the program to
> choose to exit
> gracefully (perhaps complaining to the user about the
> non-numeric input
> first) or otherwise respond to the situation without
> crashing.
> 

My understanding of what Asheesh said is that 
bucket=int(input)
does no error checking allowing me to do the checking myself and 
respond to an error myself instead of having the program crash.
..but such is not the case! (as you point out.)

So I guess I'll just NOT do error checking for the time being.
If it's not too complex perhaps Asheesh will teach how to do this 
on Monday evening.

> p.s.  Hope I'm not intruding on this thread.  I'm
> not in the class, mainly
> because I'm already reasonably familiar with both
> Python and software
> development (and because it's hard to fit into my
> schedule).
> 

Intruding?!!
Quite the contrary!
Teach us if you will.


> On Sat, Oct 11, 2008 at 7:13 AM, Alex Kleider
> <a_kleider at yahoo.com> wrote:
> 
> >
> > --- On Sat, 10/11/08, Asheesh Laroia
> <asheesh at asheesh.org> wrote:
> >
> > > From: Asheesh Laroia <asheesh at asheesh.org>
> > > Subject: Re: [sf-lug] ThinkPython: coding problem
> > > To: "Linux userGroup"
> <sf-lug at linuxmafia.com>
> > > Date: Saturday, October 11, 2008, 1:40 AM
> > > On Fri, 10 Oct 2008, Alex Kleider wrote:
> > >
> > > > # ThinkPython: question-
> > > >
> > > > As part of our ThinkPython class exercises I
> want to
> > > > check user input.
> > > > i.e. the user is asked to enter a number and
> I want
> > > the routine
> > > > to check that it is indeed a number that is
> entered.
> > > > It seems that everything
> "collected" by
> > > raw_input is considered
> > > > a string, even if only digits are included.
> > >
> > > You can always convert a string to an int by
> doing:
> > >
> > > >>> inputted_thing = '3'
> > > >>> type(inputted_thing)
> > > <type 'str'>
> > > >>> as_number = int(inputted_thing)
> > > >>> type(as_number)
> > > <type 'int'>
> > >
> > > Note that this does no error checking.  In Python
> you would
> > > typically do
> > > that with exceptions, and I don't think
> we've
> > > gotten there yet.
> > >
> > > -- Asheesh.
> > >
> >
> > When I run the following program:
> >
> > response=raw_input('Please enter a number: ')
> > response_as_number=int(response)
> > print type(response_as_number)
> >
> > If the input is a letter rather than a digit, it
> crashes:
> >
> > alex at ibmtp:~/Python/Ex$ python test_al
> > Please enter a number: p
> > Traceback (most recent call last):
> >  File "test_al", line 2, in <module>
> >    response_as_number=int(response)
> > ValueError: invalid literal for int() with base 10:
> 'p'
> > alex at ibmtp:~/Python/Ex$
> >
> > It seems there is error checking going on.
> >
> >
> >
> >
> >
> >
> > _______________________________________________
> > sf-lug mailing list
> > sf-lug at linuxmafia.com
> > http://linuxmafia.com/mailman/listinfo/sf-lug
> >
> _______________________________________________
> sf-lug mailing list
> sf-lug at linuxmafia.com
> http://linuxmafia.com/mailman/listinfo/sf-lug


      




More information about the sf-lug mailing list