Avoiding a SyntaxError when giving no input

Alex Martelli aleax at aleax.it
Wed Jan 16 08:44:02 EST 2002


"Heiko Wolf" <heiko.wolf at dlr.de> wrote in message
news:d013da35.0201160502.4b9ea383 at posting.google.com...
> Hi,
>
> I got a python script here that takes strings with the input() -
> function.
> Now, if I dont enter anything, and just press enter, python exits with
> a SyntaxError.
> How can I force python to just ask again for a value?
> I tried it with exception handling, but that doesnt seem to work with
> SyntaxError.

Sure it works:

C:\Python22>python
Python 2.2 (#28, Dec 21 2001, 12:21:22) [MSC 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> try: x=input()
... except SyntaxError: print "hey, a syntax error"
...

hey, a syntax error
>>>


Alex






More information about the Python-list mailing list