flying-wyf

python编程快速上手之第15章实践项目参考答案(15.12.1)

#! python3
# stopwatch.py - A simple stopwatch program.

import time,pyperclip

# Display the program's instructions.
print('Press enter to begin. Afterwards, press ENTER to "click" the stopwatch. Press Ctrl-C to quit.')
input() # press Enter to begin
print('Started.')
startTime = time.time() # get the first lap's start time
lastTime = startTime
lapNum = 1
# Start tracking the lap times.
try:
    while True:
        input()
        lapTime = str(round(time.time() - lastTime, 2))
        totalTime = str(round(time.time() - startTime, 2))
        output=('Lap #%s: %s (%s)' % (str(lapNum).rjust(2), totalTime.rjust(5), lapTime.rjust(5))) 
        print(output, end='')        
        lapNum += 1
        lastTime = time.time() # reset the last lap time
except KeyboardInterrupt:
    # Handle the Ctrl-C exception to keep its error message from displaying.
    print('\nDone.')
    pyperclip.copy(output)

 

posted on 2017-05-21 21:42  flying-wyf  阅读(447)  评论(1编辑  收藏  举报

导航