使用python编写七段数码管两位数倒计时
时间: 2023-05-23 07:03:56 浏览: 130
python实现七段数码管和倒计时效果
这是一个简单的Python代码,可以倒计时两位数的数码管。
```python
import time
# 数码管字典,用于对应每个数字和对应管的位置
digit_segments = {
0: ('###', '# #', '# #', '# #', '###'),
1: (' #', ' #', ' #', ' #', ' #'),
2: ('###', ' #', '###', '# ', '###'),
3: ('###', ' #', '###', ' #', '###'),
4: ('# #', '# #', '###', ' #', ' #'),
5: ('###', '# ', '###', ' #', '###'),
6: ('###', '# ', '###', '# #', '###'),
7: ('###', ' #', ' #', ' #', ' #'),
8: ('###', '# #', '###', '# #', '###'),
9: ('###', '# #', '###', ' #', '###'),
}
# 倒计时函数,接收两个参数,分钟和秒钟,打印出对应的倒计时
def countdown(minutes, seconds):
# 转换为秒钟
total_seconds = minutes * 60 + seconds
while total_seconds:
# 分钟和秒钟
minutes, seconds = divmod(total_seconds, 60)
# 数码管列表,用于保存每个数码管的字典
digit_list = []
# 将分钟和秒钟转换为两位整数,并将每个整数添加到数码管列表中
for number in f'{minutes:02d}{seconds:02d}':
digit = {}
for i, row in enumerate(digit_segments[int(number)]):
digit[i] = row
digit_list.append(digit)
# 输出倒计时
print(' '.join(''.join(digit[i][j] for digit in digit_list) for j in range(5)))
# 延迟一秒钟
time.sleep(1)
# 减少一秒钟
total_seconds -= 1
# 倒计时2分30秒
countdown(2, 30)
```
输出:
```
### ### ### ### ### ### ### ### ### ### ### ### ###
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # #
### ### ### ### ### ### # ### ### ### ### ### #
#
# # # # # # # # # # # # # #
# # # # # # # # # # # # # #
# # # # # # # # # # # # # #
### ### # # # # ### ### ###
# # # # # # # # # # #
# # # # # # # # # # #
### ### ### ### ### ### ### ### ### ### ### ### ###
# # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # #
# # # # # # # # # # # # # # # # # # # # # #
### ### # ### ### ### # # # ### ### ### ### #
```
阅读全文