[sf-lug] ThinkPython: coding problem

Asheesh Laroia asheesh at asheesh.org
Sat Oct 11 01:40:21 PDT 2008


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.

-- 
Lazlo's Chinese Relativity Axiom:
 	No matter how great your triumphs or how tragic your defeats --
 	approximately one billion Chinese couldn't care less.




More information about the sf-lug mailing list