15.12.1 美化的秒表

扩展本章的秒表项目,让它利用 rjust()和 ljust()字符串方法来“美化”的输出。 (这些方法在第 6 章中介绍过)。输出不是像这样:
这里写图片描述
而是这样
这里写图片描的描述

请注意,对于 lapNum、lapTime 和 totalTime 等整型和浮点型变量,你需要字符 串版本,以便对它们调用字符串方法。接下来,利用第 6 章中介绍的 pyperclip 模块, 将文本输出复制到剪贴板,以便用户可以将输出快速粘贴到一个文本文件或电子邮 件中。

#! 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
L=[]
# Start tracking the lap times.
try:
    while True:
        input()
        lapTime = round(time.time() - lastTime, 2)
        totalTime = round(time.time() - startTime, 2)
        #输出的结果result
        result='Lap #%s:%s ( %s)' % (str(lapNum).rjust(3), str(totalTime).rjust(6), str(lapTime).ljust(5)) 
        print(result,end='')
        L.append(result)#将结果添加到列表中
        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('\n'.join(L))#将结果存放到剪贴板中
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值