Error checking for 'raw_input( )'?

Alex Martelli aleaxit at yahoo.com
Thu Jun 7 05:37:47 EDT 2001


"eric_brake" <brakedon at hotmail.com> wrote in message
news:7b515e0f.0106061427.2386067f at posting.google.com...
    ...
> > Is this the sort of thing you want to do...?  Of course,
    ...
> not really, I was hoping there was a way like when I use a "input()"
> field and someone tries to enter a letter or word. Of course a error
> occurs then I can catch them with try/except statements. but since

But it's not necessarily an error -- if the 'letter or word' they
enter happens to be the name of a variable in current scope, for
example.  input() accepts any valid expression and such a 'letter
or word' may well be a valid expression.

> "raw_input()" can accept any string, letters or numbers, it doesn't
> error.

So check the string that you've just been given, e.g. by
such approaches as I just mentioned.

Or, if you want the string you're given to be a correct
representation for an integer, for example, the check
is trivial:

    thestring = raw_input()
    try: thenumber = int(thestring)
    except ValueError: diagnose_mistake()

for example.  Is THIS what you want to do...?  I'm having
a somewhat hard time understanding your specs, I'll admit,
so making them clearer might help.


Alex






More information about the Python-list mailing list