[sf-lug] ThinkPython: coding problem
Jeff Bragg
jackofnotrades at gmail.com
Sat Oct 11 07:50:15 PDT 2008
Actually, there's nothing stopping you from catching and responding to the
built-in exceptions, too (except, of course, that you guys haven't covered
exceptions yet).
On Sat, Oct 11, 2008 at 7:45 AM, Jeff Bragg <jackofnotrades at gmail.com>wrote:
> It appears to me that it crashed due to a type clash. 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.
>
> 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).
>
>
> 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
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://linuxmafia.com/pipermail/sf-lug/attachments/20081011/91add36a/attachment.html>
More information about the sf-lug
mailing list