the button of cancel

Fredrik Lundh fredrik at pythonware.com
Sun Nov 13 04:16:46 EST 2005


Ben Bush wrote:

> When I click the button of cancel,
> I got the follwoing message. I want to see "Goodbye" on the console screen.
> What to do?

> KeyboardInterrupt: operation cancelled

> num = input( "Enter a number between 1 and 100: " )
> while num < 1 or num > 100:
>    print "Oops, your input value (", num, ") is out of range."
>    num = input( "Be sure to enter a value between 1 and 100: " )

you need to catch the exception, and print the message you want.

    try:
        ... your code ...
    except KeyboardInterrupt:
        print "Goodbye!"

see chapter 8.3 in the tutorial for more info:

    http://docs.python.org/tut/node10.html

</F> 






More information about the Python-list mailing list