[Tutor] How to exit this loop in the interpreter

Simon Yan simonyan at fedoraproject.org
Thu May 3 16:22:10 CEST 2012


On Thu, May 3, 2012 at 9:57 PM, <spawgi at gmail.com> wrote:

> Hello all,
>
> I have encountered the following scenario.
> Here is the code - on IDLE on Windows XP.
>
> *>>> while True:
>     try:
>         number = raw_input("enter number - ")
>         print number * number
>     except ValueError:
>         print "invalid number"
>     except:
>         print "unspecified exception"
>     else:
>         print "other conditions"
>
>
> enter number - 3
> unspecified exception
> *
> What I noticed is that no matter, what input I give, I cannot exit this
> loop. I have tried control-C, control-D etc. all the keys. So how can I
> exit from this loop?
>

If you simply want to stop after an exception was caught, you can insert
"break" after each print line. Like below:
>>> while True:
    try:
        number = raw_input("enter number - ")
        print number * number
    except ValueError:
        print "invalid number"
        break
    except:
        print "unspecified exception"
        break
    else:
        print "other conditions"
        break


>
> Thanks and Regards,
> Sumod
>
> --
> http://spawgi.wordpress.com
> We can do it and do it better.
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
Regards,
YeeYaa (Simon Yan)

http://simonyan.fedorapeople.org/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120503/09007805/attachment-0001.html>


More information about the Tutor mailing list