Python模拟电梯数码管显示编程 假设你要乘坐电梯,假设只有一部电梯,其显示面板上有七段数码管显味 电梯当前所在楼层。已知数码管各段按照课本P138编号,假设你的学号为M, 且第(M%7+1)段数码管坏了(一直是亮的),问题:你现在在1楼,电梯停在 (M与N均小于等于9)请模拟你按了电梯后,到你到达 M楼,你要去N楼, N楼这个过程中,显示面饭上七段数码管的显示过程(假设这期间没有其他人 按电梯)
时间: 2024-03-06 17:47:09 浏览: 15
好的,根据您的要求,我来为您编写 Python 模拟电梯数码管显示的程序。请您先输入您的学号 M 和您要去的 N 楼层:
```python
M = int(input("请输入您的学号 M:"))
N = int(input("请输入您要去的楼层 N:"))
```
接下来,我们需要定义一个函数来模拟电梯的运行过程,其中包括电梯运行的方向、当前楼层、显示面板的显示等内容。具体实现如下:
```python
def elevator(M, N):
current_floor = 1 # 当前楼层
direction = 1 # 电梯运行的方向,1 表示向上,-1 表示向下
segments = [0, 0, 0, 0, 0, 0, 0] # 数码管显示面板,0 表示灭,1 表示亮
while current_floor != M: # 等待电梯到达 M 楼层
if current_floor < M:
direction = 1
current_floor += 1
else:
direction = -1
current_floor -= 1
# 数码管显示面板的显示
segments[M % 7] = 1 # 坏掉的数码管
segments[int(current_floor / 10)] = 1 # 十位数码管
segments[current_floor % 10] = 1 # 个位数码管
print(segments)
segments[M % 7] = 0 # 将坏掉的数码管恢复为灭
segments[int(current_floor / 10)] = 0 # 恢复十位数码管
segments[current_floor % 10] = 0 # 恢复个位数码管
# 数码管显示面板的显示
segments[M % 7] = 1 # 坏掉的数码管
segments[int(current_floor / 10)] = 1 # 十位数码管
segments[current_floor % 10] = 1 # 个位数码管
print(segments)
# 电梯运行到 N 楼层
while current_floor != N:
if current_floor < N:
direction = 1
current_floor += 1
else:
direction = -1
current_floor -= 1
# 数码管显示面板的显示
segments[int(current_floor / 10)] = 1 # 十位数码管
segments[current_floor % 10] = 1 # 个位数码管
print(segments)
segments[int(current_floor / 10)] = 0 # 恢复十位数码管
segments[current_floor % 10] = 0 # 恢复个位数码管
# 数码管显示面板的显示
segments[int(current_floor / 10)] = 1 # 十位数码管
segments[current_floor % 10] = 1 # 个位数码管
print(segments)
```
最后,我们只需要调用这个函数,并传入参数 M 和 N,程序就会模拟电梯的运行过程,并显示数码管的显示过程:
```python
elevator(M, N)
```
希望这个程序能够满足您的需求。如果您还有其他问题,欢迎随时询问!