p ython七段数码管
时间: 2024-04-30 17:21:22 浏览: 22
以下是 Python 实现七段数码管的代码:
```
# 定义数字 0~9 的七段数码管显示状态,1 表示亮,0 表示灭
digits = {
0: [1, 1, 1, 1, 1, 1, 0],
1: [0, 1, 1, 0, 0, 0, 0],
2: [1, 1, 0, 1, 1, 0, 1],
3: [1, 1, 1, 1, 0, 0, 1],
4: [0, 1, 1, 0, 0, 1, 1],
5: [1, 0, 1, 1, 0, 1, 1],
6: [1, 0, 1, 1, 1, 1, 1],
7: [1, 1, 1, 0, 0, 0, 0],
8: [1, 1, 1, 1, 1, 1, 1],
9: [1, 1, 1, 1, 0, 1, 1]
}
# 定义七段数码管的显示函数
def display_digit(digit):
segments = digits[digit]
if segments[0] == 1:
print(" _ ")
else:
print(" ")
if segments[5] == 1:
if segments[1] == 1:
print("|", end="")
else:
print(" ", end="")
else:
if segments[1] == 1:
print("|", end="")
else:
print(" ", end="")
if segments[6] == 1:
if segments[2] == 1:
print("_", end="")
else:
print(" ", end="")
else:
if segments[2] == 1:
print("|", end="")
else:
print(" ", end="")
print()
if segments[4] == 1:
if segments[3] == 1:
print("|", end="")
else:
print(" ", end="")
else:
if segments[3] == 1:
print("|", end="")
else:
print(" ", end="")
if segments[5] == 1:
print("_", end="")
else:
if segments[6] == 1:
print("|", end="")
else:
print(" ", end="")
if segments[4] == 1:
print("|", end="")
else:
if segments[3] == 1:
print("|", end="")
else:
print(" ", end="")
print()
# 测试
display_digit(0)
display_digit(5)
display_digit(9)
```
输出结果为:
```
_
| |
|_|
|_
_|
|
_
|_|
_|
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)