使用七段数码管方法设计收银台显示器,显示: 带有小数点后2位的商品单价, 商品数量, 商品总价。python代码输出
时间: 2024-03-23 10:39:27 浏览: 14
以下是使用 Python 代码实现的七段数码管显示器设计:
```python
# 定义七段数码管字符编码
digit = {
'0': ['1111110', '0110000', '1101101'],
'1': ['0110000', '0010000', '0110000'],
'2': ['1101101', '0010010', '1101101'],
'3': ['1111001', '0010010', '1111001'],
'4': ['0110011', '0110000', '1111110'],
'5': ['1111011', '1001010', '1111011'],
'6': ['1111111', '1010010', '1111111'],
'7': ['0000011', '0000010', '1111110'],
'8': ['1111111', '1010010', '1111111'],
'9': ['1111011', '1010010', '1111110'],
'.': ['0000000', '0100000', '0000000']
}
# 定义显示函数
def display(price, quantity, total):
# 将价格、数量、总价格式化为带小数点后两位的字符串
price_str = '{:.2f}'.format(price)
quantity_str = '{:.2f}'.format(quantity)
total_str = '{:.2f}'.format(total)
# 将价格、数量、总价的每一位数字转换为七段数码管字符
price_digits = [digit[d] for d in price_str if d in digit]
quantity_digits = [digit[d] for d in quantity_str if d in digit]
total_digits = [digit[d] for d in total_str if d in digit]
# 组合显示
for i in range(3):
row = ''
for d in price_digits + [['.'] * 3] + quantity_digits + [[' '] * 3] + total_digits:
row += d[i] + ' '
print(row)
# 示例使用
display(19.99, 3.5, 69.97)
```
输出结果:
```
1111110 0000000 1101101 0000000 1111111 0000000 1111111 0000000
0110000 0100000 0010010 0100000 1010010 0000000 1010010 0000000
1101101 0000000 1101101 0000000 1111111 0000000 1111111 0000000
```