Question: Please study the exception handling in the Python program below: import time def do something (): print (...press Crtl+C to stop', flush=True) time.sleep (0.5)

Please study the exception handling in the Python program below: import time def do something (): print (...press Crtl+C to stop', flush=True) time.sleep (0.5) #wait for 0.5 second interval while True: try: do something () except KeyboardInterrupt: print('crtl+C is detected') break print ('done') Extend the above Python program to handle keyboard interrupts to produce the following output effects. Below shows 3 separate runs with their sample outputs. In this program, each number (0,1,2,3) is displayed 6 times starting from left to right until "done". Each number is displayed for 0.5s interval before the next one is displayed, on the same line. Basically, the program prints out the 4 numbers for 6 times each, starting from 0. After one number completes 6 repetitions, the next number in sequence will repeat for 6 times, provided that no keyboard interrupt is pressed. But if during repetition, a keyboard interrupt (ctrl-C) is pressed, then the current number displayed will change to the next number which has not completed its 6 total appearances. Program terminates when every number has been displayed for 6 times each in total, and "done" is displayed, followed by calculation results shown below. Output: 0 0 0 0 0 0 1 1 1 1 2 2 2 2 2 2 3 3 3 < 1 1 3 3 3 done Number of ctrl+C presses = 2 Average clock counts of [6, 4, 6, 3, 2, 3] = 4.0 Count: Frequency (sorted in ascending) 4 : 1 2: 1 6 : 2 3: 2 NNH (First, a ctrl+C is pressed here) (Then, another ctrl+C is pressed here)
Step by Step Solution
3.47 Rating (160 Votes )
There are 3 Steps involved in it
To extend the given Python program to handle keyboard interrupts and produce the desired output ... View full answer
Get step-by-step solutions from verified subject matter experts
