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...
-
Earthworks Health Co. is an HMO for 12 businesses in the St. Louis area. The following account balances appear on the balance sheet of Earthworks Health Co.: Common stock (400,000 shares authorized),...
-
Two 12-meter boats, the K boat and the L boat, are tested as possible contenders in the America's Cup races. The following data represent the time, in minutes, to complete a particular track in...
-
\(4+27\) Use clock arithmetic \((\bmod 12)\), to perform the indicated calculation.
-
At the beginning of 2016, the Redd Company had the following balances in its accounts: Cash ........ $ 6,900 Inventory ....... 15,000 Land ......... 7,000 Common stock .... 15,000 Retained earnings...
-
Need the answer for questions 1-6 (highlighted). 1. AA rated bond spreads: 0.50% 2. AA spread change: 0.00% (-) tighten / (+) widen Term Structure of Rates 3. UST yield curve shift: 0.00% (-) rally /...
-
Cheapest Car Rental rents cars at the Chicago airport. The car rental market consists of two segments: the short-term segment, which rents for an average of 0.5 week, and the medium-term segment,...
-
A 24m high concrete gravity dam will be constructed with the dimensions shown in the figure below. The dam is composed of three blocks of equal heights. It is intended to implement an internal...
-
What are the key factors that influence ethical decision-making at both individual and organizational levels, and how do these factors interplay to shape organizational outcomes ?
-
== Carry out policy iteration over the MDP example covered in class with R given in Table 2 and 0.9. For a state s, if R(s) = 1, s is a terminal state. For the transition model, assume that the agent...
-
List and describe 3 key macroeconomic factors ( gross domestic product, inflation, employment ) for firms to examine as part of their external analysis. Explain how macroeconomic factors influence...
-
This week, one of the topics we have been looking at IPO stock issuance and the associated costs. Firms going to public markets through stock issuance incur a variety of costs, some are clear cut,...
-
Open addition.py and look at the definition of add : def add(a, b): "Return the sum of a and b" "*** YOUR CODE HERE ***" return 0 Copy The tests called this with a and b set to different values, but...
-
Patrick Star Enterprises has a 15-year bond issue outstanding that pays a 9 percent coupon. The bond is currently priced at $894.60 and has a par value of $1,000. Interest is paid semiannually. What...
-
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...
-
The balance sheet of Hubble Ltd as at 31 May 2010 is shown below. Hubble Ltd agreed to purchase at this date the freehold premises, plant and machinery and stock _ of A Bubble at agreed valuations of...
-
From the following information you are required to prepare a statement apportioning the ~ unappropriated profit between the pre-incorporation and post-incorporation periods, showing the basis of...
-
Rowlock Ltd was incorporated on 1 October 2008 to acquire Rowlocks mail order business, with effect from 1 June 2008. The purchase consideration was agreed at 35,000 to be satisfied by the issue on 1...
Study smarter with the SolutionInn App