用python写一个计时器
时间: 2024-05-10 19:15:17
浏览: 159
Python 实现一个计时器
以下是一个简单的计时器的[python](https://wenku.csdn.net/doc/6412b46ebe7fbd1778d3f92a?spm=1055.2569.3001.10083)程序:
```python
import time
def countdown(t):
[while](https://wenku.csdn.net/doc/645cade695996c03ac3eb2ce?spm=1055.2569.3001.10083) t:
mins, secs = divmod(t, 60)
timer = '{:02d}:{:02d}'.[format](https://wenku.csdn.net/doc/645b7ad7fcc53913682b861a?spm=1055.2569.3001.10083)(mins, secs)
print(timer, end="\r")
time.[sleep](https://wenku.csdn.net/doc/6412b53ebe7fbd1778d42752?spm=1055.2569.3001.10083)(1)
t -= 1
print('Time is up!')
t = input("Enter the time in [second](https://wenku.csdn.net/doc/64618157543f84448893382a?spm=1055.2569.3001.10083)s: ")
countdown(int(t))
这个程序要求用户输入一个时间(以秒为单位),然后开始倒计时。当计时器归零时,程序会输出“Time is up!”。
阅读全文