Please study the exception handling in the Python program below: import time def do something ():...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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) 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)
Expert Answer:
Answer rating: 100% (QA)
To extend the given Python program to handle keyboard interrupts and produce the desired output ... View the full answer
Related Book For
Statistics The Exploration & Analysis Of Data
ISBN: 9780840058010
7th Edition
Authors: Roxy Peck, Jay L. Devore
Posted Date:
Students also viewed these programming questions
-
A state runs a lottery in which 6 numbers are randomly selected from 40, without replacement. A player chooses 6 numbers before the states sample is selected. (a) What is the probability that the 6...
-
Repeat Prob. 9.56 except this time let the odd coincidence be that the static pressure downstream of the shock exactly equals the throat pressure. What is the area where the shock wave stands? sonic...
-
Repeat Exercise 18.2 when 1 of the: next 4 drinks overflows and the uniform prior distribution is (p) = 10, 0.05 < p < 0.15.
-
An investor bought a 70-strike European put option on an index with six months to expiration.The premium for this option was 1. The investor also wrote an 80-strike European put optionon the same...
-
Sometimes economists speak of the certainty equivalent of a risky stream of income. This problem asks you to compute the certainty equivalent of a risky bet that promises a 50-50 chance of winning or...
-
Employees are requesting that you turn off the screen that displays customer checkout time or add more cashiers and baggers to each shift to reduce wait times. What action would you take to address...
-
Identify at least three grounds a party may have for objecting to interrogatories.
-
The Fair View Golf & Country Club details the following accounts in its financial statements. Instructions(a) Classify each of the above accounts as an asset (A), liability (L), stockholders?? equity...
-
Question 30 1 Point An online toy store has a directive that "Failure to deliver the ordered toy within three hours will lead to issuing a warning followed by dismissal if the act is repeated"....
-
Gibson Agency Case: 1. Calculate and present the budgeted profit for each of Gibson's clients for each of the years 2016 through 2019, using the current costing system (i.e., the one described in the...
-
An Assembly consists of the following elements as given below. ( Task times are in minutes. ) The production rate required is one assembly every 1 5 minutes. Find the balance that minimizes the...
-
Critically explain the major types of accounts that are offered to the customers in a banking industry (15 marks).
-
(a) Discuss the Four (4) causes of demand push inflation (8 marks). (b) Explain the factors that determine money supply in the economy of Kenya (7 marks).
-
a) Discuss the expected utility hypothesis in the context of demand for insurance. Use illustration where necessary. [6 Marks] b) Differentiate between moral hazard and adverse selection as...
-
Question 40 (1 point) The cost of investment is $1000 at time 0, the cash flow in year one is $2500, cash flow in year two is -$3000 (negative cash flow), and the cash flow is $4000 in year three....
-
af 1) Given f(x, y) = x', which of the following is the mixed second order partial 7 @yax derivative of ? J-1 (a) (1+ylnx) '(b) yx' + x' (c) y(y-1)x2 (d) yx' (e) x+xln x [10 Marks] 2) What is the...
-
First, create a 6 sentence story of your life: you are the protagonist. #2 is where you meet your antagonist, #4 is where your antagonist causes you to change.... 1. Once upon a time *2 One day 3 And...
-
The vapor pressure of the liquid NH, is measured at different temperatures. The following vapor pressure data are obtained. Temperature, K P, mmHg 217.1 223.4 234.7 588.1 Calculate the enthalpy of...
-
The paper The Curious Promiscuity of Queen Honey Bees (Apis mellifera): Evolutionary and Behavioral Mechanisms (Annals of Zoology Fennici [2001]:255 265) describes a study of the mating behavior of...
-
If you were taking a new job and had your choice of a boss, would you prefer to work for a man or a woman? That was the question posed to individuals in a sample of 576 employed adults (Gallup at a...
-
The paper If Its Hard to Read, Its Hard to Do (Psychological Science [2008]: 986988) described an interesting study of how people perceive the effort required to do certain tasks. Each of 20 students...
-
Calculate the acentric factor for ethanol. The vapour pressure of ethanol can be estimated from the following equation: \[ \log _{10} P^{\text {Sat }}=8.1122-\frac{1592.864}{t+226.184} \] where...
-
For liquid acetone at \(20^{\circ} \mathrm{C}\) and \(1 \mathrm{bar}\), \[ \beta=1.487 \times 10^{-3} /{ }^{\circ} \mathrm{C} \quad \alpha=62 \times 10^{-6} / \mathrm{bar} \quad V=1.287...
-
Determine the molar volume of ammonia vapour and ammonia liquid at \(321.55 \mathrm{~K}\) and 1.95 \(\mathrm{MPa}\). Ammonia is assumed to follow van der Waals equation of state.
Study smarter with the SolutionInn App