On 11/13/05, Fredrik Lundh <[EMAIL PROTECTED]> wrote:
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>
Thanks, now the code works. I wonder when i put the number between 1 and 100, I get the message of 'Your number is in the range' but i still see the input box. How to remove it?
num = input( "Enter a number between 1 and 100: " )
try:
    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: ' )
    num=input('Your number is in the range')
except KeyboardInterrupt:
    print 'Goodbye!'
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to