• 技术文章 >Python技术 >Python基础教程

    python编程实战:制作秒表程序

    宋雪维宋雪维2020-12-04 17:42:19原创4455

    现如今生活节奏的加快,再加个人们对营养的需求也是在不断加大。我们平时所食用的食物只有在它烹饪到刚刚好的时候才会把它自身的营养充分的发挥出来,可是我们一般对于它的时间方面不是很好把握,所以对于这一点,秒表计时器就帮了我们很大的忙。秒针在生活中一般用作精确计时,作用很大。那你知道用我们万能的python怎么做秒表吗?下面我们来看看吧~

    代码:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    <p style="line-height: normal;">#! python3

     

    # stopwatch.py - A simple stopwatch program.

     

    import time

     

    # 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 = round(time.time() - lastTime, 2)

     

      totalTime = round(time.time() - startTime, 2)

     

      print('Lap #%s: %s (%s)' % (lapNum, totalTime, lapTime), 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.')<br></p>

    以上就是python制作秒表程序的过程,看起来复杂,但其实很简单,快试试吧~

    专题推荐:python编程秒表python实战
    上一篇:python字典中values方法使用 下一篇:python all函数怎么用

    相关文章推荐

    • python ord函数如何返回ascii码?• python中eval函数如何使用?• 如何使用python replace()方法?

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网