[Tutor] exception about ctrl+c
I want to catch the ctrl+c exception. My program is as following. But when I run my script and press ctrl+c, the program output nothing. I don't know where did I go wrong. Please help me. Thank you! def safe_input(prompting): try: return raw_input(prompting); except KeyboardInterrupt, error: print error; return None; def main(): a = safe_input(input any thing!\n); print a; if __name__ == '__main__': main(); daedae11___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exception about ctrl+c
On Mon, Jan 9, 2012 at 7:24 AM, daedae11 daeda...@126.com wrote: I want to catch the ctrl+c exception. My program is as following. But when I run my script and press ctrl+c, the program output nothing. I don't know where did I go wrong. Please help me. Thank you! def safe_input(prompting): try: return raw_input(prompting); except KeyboardInterrupt, error: print error; return None; def main(): a = safe_input(input any thing!\n); print a; if __name__ == '__main__': main(); daedae11 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor python ctl-c.py input any thing! fdsaj fdsaj python ctl-c.py input any thing! ^C None I just got this from your code. Seems to work on python 2.65, linux Are you running it from a command line like: python ctl-c.py or are you running in a python shell? If you are in a shell it might be consuming the ctl-c before your program can -- Joel Goldstick ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] exception about ctrl+c
On 2012/01/09 02:24 PM, daedae11 wrote: I want to catch the ctrl+c exception. My program is as following. But when I run my script and press ctrl+c, the program output nothing. I don't know where did I go wrong. Please help me. Thank you! def safe_input(prompting): try: return raw_input(prompting); except KeyboardInterrupt, error: print error; return None; def main(): a = safe_input(input any thing!\n); print a; if __name__ == '__main__': main(); daedae11 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor def safe_input(prompting): try: return raw_input(prompting) except KeyboardInterrupt: print 'KeyboardInterrupt Issued' return None That will work as intended, if you had your original `except KeyboardInterrupt, error:` and did a `print repr(error)` afterwards you will see it does not contain an error message as you perhaps wanted. Also, Python does not require semi-colons to denote the end-of-line. It can be used if you want to have multiple statements on a single line though. -- Christian Witts Python Developer // ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor