[Tutor] keyboard interrupt

Dave Angel davea at davea.name
Thu May 23 03:25:57 CEST 2013


On 05/22/2013 04:11 PM, Jerry Hill wrote:
> On Wed, May 22, 2013 at 3:47 PM, Jim Mooney <cybervigilante at gmail.com>wrote:
>
>> I made a simple ear frequency-tester, but I don't want it to go on
>> forever, so I tried stopping it when I  pressed a key, as below, but
>> that doesn't work. I did check out keyboard interrupts but they seem
>> unnecessarily complex just to stop a program. I'm not passing keys. Is
>> there something simple I'm missing?
>>
>> import winsound
>>
>> try:
>>      for freq in range(100,32000,100):
>>          winsound.Beep(freq, 1000)
>> except KeyboardInterrupt:
>>      pass
>>
>>
> The KeyboardInterrupt ​exception is raised when someone presses Ctrl-C.  If
> you catch it, and ignore it (which is what your code above is doing), then
> pressing Ctrl-C doesn't do anything.  If you just take out the try/except,
> then you can hit Ctrl-C and interrupt your program as normal.
>

What do you mean "doesn't do anything" ?  It certainly terminates the 
loop, which was the intent.  Provided of course that something else 
isn't trapping the Ctrl-C first.






-- 
DaveA


More information about the Tutor mailing list