Loop exception catching

Aldo Cortesi aldo at nullcube.com
Mon Jan 23 21:40:42 EST 2006


Thus spake Ivan Shevanski (darkpaladin79 at gmail.com):

> Alright this is kind of a continuation of a past conversation. . .But not
> really.  Anyway, heres the problem.  Say I had this:
> 
>         try:
>             x = input("> ")
>         except SyntaxError:
>            print "explain the problem here"
>            x = input("> ")
> 
> That's to catch when people just press enter by mistake without selecting
> something from the menu.  But the thing is how would I make that loop? I
> want it to keep catching the user pressing enter if they did it over and
> over [there are some weird people out there ;)] I tryed this but to no
> avail:
> 
>         try:
>             x = input("> ")
>         except SyntaxError:
>             while SyntaxError:
>                 print "Please enter only the number beside your choice"
>                 print
>                 x = input("> ")

Well, leaving aside the merits of using "input" (which should be avoided at all
costs), here's one way to do what you want:

while 1:
    try:
        x = input("> ")
        break
    except SyntaxError:
        print "explain the problem here"




Cheers,



Aldo



-- 
Aldo Cortesi
aldo at nullcube.com
http://www.nullcube.com
Mob: 0419 492 863



More information about the Python-list mailing list