
在构建电梯模拟系统时,一个常见需求是将建筑物的大堂层(通常标记为0层)作为起始点。这与一些将1层作为最低层的系统有所不同。本教程将指导您如何在现有python电梯模拟代码的基础上,实现0层作为起始楼层的逻辑,并确保楼层显示和到达信息的准确性。
核心电梯运行逻辑回顾
首先,我们来看一下原始的电梯上下楼函数,它们构成了电梯模拟的核心:
def goDownfloor(current, target):
for floor in range(current, target, -1):
current -= 1
if floor != target + 1:
print(f"current floor is {current}.")
else:
print(f"Arrived at the {target} . Goodbye.")
return current
def goUpfloor(current, target):
for floor in range(current, target):
current += 1
if floor != target - 1:
print(f"current floor is {current}.")
else:
print(f"Arrived at the {target} . Goodbye.")
return current这两个函数分别处理电梯向下和向上移动的逻辑。关键在于range函数的用法以及内部的条件打印语句:
- goDownfloor: range(current, target, -1) 从当前楼层向下迭代,直到 target + 1 停止(不包含 target + 1)。在循环内,current -= 1 更新实际楼层。
- goUpfloor: range(current, target) 从当前楼层向上迭代,直到 target - 1 停止(不包含 target - 1)。在循环内,current += 1 更新实际楼层。
- if floor != target + 1 和 if floor != target - 1:这些条件语句用于控制打印时机。它们确保在电梯到达目标楼层的前一刻,打印的是中间楼层信息,而当循环即将结束(即 floor 达到 target + 1 或 target - 1 时),则打印“Arrived”信息。
实现0层起始逻辑
要使电梯从0层开始,最直接且有效的方法是修改程序的初始 currentFloor 变量。许多开发者可能会尝试在 goUpfloor 或 goDownfloor 函数内部进行复杂的修改,但实际上,原始的迭代逻辑已经足够灵活,可以处理0层。
解决方案: 将主程序中的 currentFloor 初始值设置为 0 即可。
currentFloor = 0 # 将起始楼层设置为0
为什么这样做有效?
立即学习“Python免费学习笔记(深入)”;
让我们以从0层到3层为例进行分析:
- 初始 currentFloor = 0。
- 用户输入 targetFloor = 3。
- 调用 goUpfloor(0, 3)。
- for floor in range(0, 3):
-
第一次迭代: floor 为 0。
- current += 1 使得 current 变为 1。
- if 0 != 3 - 1 (即 0 != 2) 为真,打印 "current floor is 1."
-
第二次迭代: floor 为 1。
- current += 1 使得 current 变为 2。
- if 1 != 3 - 1 (即 1 != 2) 为真,打印 "current floor is 2."
-
第三次迭代: floor 为 2。
- current += 1 使得 current 变为 3。
- if 2 != 3 - 1 (即 2 != 2) 为假,执行 else 块,打印 "Arrived at the 3 . Goodbye."
-
第一次迭代: floor 为 0。
- 函数返回 current (即 3),更新主循环中的 currentFloor。
可以看到,即使从0层开始,电梯也能正确地显示经过的楼层(1层、2层),并在到达3层时显示正确的到达信息。向下移动的逻辑也类似,range(current, target, -1) 同样能正确处理包含0层的情况。
完整代码实现
以下是修改后的完整电梯模拟代码,其中起始楼层已设置为0:
def goDownfloor(current, target):
"""
模拟电梯向下运行。
Args:
current (int): 当前楼层。
target (int): 目标楼层。
Returns:
int: 到达后的当前楼层。
"""
for floor in range(current, target, -1):
current -= 1
# 判断是否为到达目标楼层前的一步,以区分打印中间楼层或到达信息
if floor != target + 1:
print(f"current floor is {current}.")
else:
print(f"Arrived at the {target} . Goodbye.")
return current
def goUpfloor(current, target):
"""
模拟电梯向上运行。
Args:
current (int): 当前楼层。
target (int): 目标楼层。
Returns:
int: 到达后的当前楼层。
"""
for floor in range(current, target):
current += 1
# 判断是否为到达目标楼层前的一步,以区分打印中间楼层或到达信息
if floor != target - 1:
print(f"current floor is {current}.")
else:
print(f"Arrived at the {target} . Goodbye.")
return current
# 初始化电梯当前楼层为0(大堂)
currentFloor = 0
while True:
try:
targetFloor = int(input(f"您当前在 {currentFloor} 层。请输入您想去的楼层(输入 -100 退出):"))
except ValueError:
print("无效输入,请输入一个整数。")
continue
if targetFloor == -100:
print("电梯服务结束。")
break
elif targetFloor == currentFloor:
print('您已在目标楼层,请重新输入其他楼层。')
elif targetFloor > currentFloor:
currentFloor = goUpfloor(currentFloor, targetFloor)
elif targetFloor < currentFloor:
currentFloor = goDownfloor(currentFloor, targetFloor)注意事项与总结
- range 函数的包容性: range(start, stop, step) 函数在Python中是左闭右开的,即包含 start 但不包含 stop。这是理解电梯逻辑的关键。当 step 为正时,range(a, b) 迭代 a 到 b-1;当 step 为负时,range(a, b, -1) 迭代 a 到 b+1。
- 楼层更新与打印顺序: 在 goUpfloor 和 goDownfloor 函数中,current += 1 或 current -= 1 发生在打印之前。这意味着每次循环打印的 current 变量,都是电梯已经移动到该楼层后的实际楼层号。
- 到达消息的精确控制: if floor != target + 1 和 if floor != target - 1 这两个条件语句是确保在电梯到达目标楼层时,能够打印出“Arrived”消息而不是中间楼层号的关键。它们利用了 range 函数在最后一次迭代时 floor 值与 target 关系的特性。
- 用户体验: 在实际应用中,可以增加更友好的提示信息,例如当前所在楼层,以及更完善的错误处理(如非数字输入)。
通过以上分析和代码示例,您应该能够清晰地理解如何在Python电梯模拟中轻松实现0层作为起始楼层的逻辑,而无需对核心算法进行复杂修改。关键在于理解 range 函数的行为以及循环内部楼层更新和信息打印的顺序。










