
提供的python代码模拟了一个简单的电梯系统,包含向上和向下移动的函数以及一个主循环来处理用户输入。
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
currentFloor = 1 # 初始楼层
while(True):
targetFloor = int(input("Enter the floor you want to go to (enter -100 for outages):"))
if targetFloor == -100:
break
else:
if targetFloor > currentFloor:
currentFloor = goUpfloor(currentFloor, targetFloor)
elif targetFloor < currentFloor:
currentFloor = goDownfloor(currentFloor, targetFloor)
elif targetFloor == currentFloor:
print('Please re-enter another floor.')这段代码的核心在于 goUpfloor 和 goDownfloor 函数。它们使用 for 循环和 range() 函数来模拟楼层的逐级变化。
打印逻辑 if floor != target - 1 (或 target + 1) 用于区分中间楼层和目标楼层,确保在到达目标楼层时打印“Arrived”消息,而在中间楼层则打印当前所在楼层。
用户希望将电梯的起始楼层设置为0(通常代表大厅或地面层),并确保在电梯移动时,能够正确显示所有经过的楼层,直到目标楼层。例如,从0层到3层,应依次显示1层、2层,然后到达3层。用户尝试将 currentFloor 设置为0,但认为失败,并尝试修改 current += 1 等逻辑。
实际上,要实现0层起始的功能,并确保现有楼层显示逻辑的正确性,只需要将初始的 currentFloor 变量设置为0即可。现有的 goUpfloor 和 goDownfloor 函数中的 range() 函数以及打印逻辑已经足够健壮,能够正确处理0作为起始楼层的情况。
立即学习“Python免费学习笔记(深入)”;
下面是修改后的完整代码:
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
currentFloor = 0 # 将初始楼层设置为0
while(True):
targetFloor = int(input("Enter the floor you want to go to (enter -100 for outages):"))
if targetFloor == -100:
break
else:
if targetFloor > currentFloor:
currentFloor = goUpfloor(currentFloor, targetFloor)
elif targetFloor < currentFloor:
currentFloor = goDownfloor(currentFloor, targetFloor)
elif targetFloor == currentFloor:
print('Please re-enter another floor.')让我们通过一个具体的例子来验证当 currentFloor = 0 时,电梯向上移动的逻辑。
场景:从0层(大厅)前往3层。
初始化: currentFloor = 0,用户输入 targetFloor = 3。
调用 goUpfloor(0, 3):
current 初始值为 0,target 为 3。
for floor in range(current, target),即 for floor in range(0, 3)。floor 将依次取值 0, 1, 2。
第一次循环 (floor = 0):
第二次循环 (floor = 1):
第三次循环 (floor = 2):
函数返回 current,即 3。currentFloor 更新为 3。
从上述流程可以看出,电梯从0层到3层的过程中,正确地显示了1层和2层,然后提示到达3层。这完全符合用户的需求。goDownfloor 函数在处理负向移动时,其 range() 和打印逻辑也同样能够正确处理包含0层的情况。
通过本次分析,我们发现将Python电梯模拟程序的起始楼层设置为0,并不需要对核心的 goUpfloor 或 goDownfloor 函数进行修改。原有的代码结构,特别是 range() 函数的运用和条件打印逻辑,已经能够很好地支持0层起始,并确保在电梯移动过程中正确显示所有中间楼层。这个案例也提醒我们,在解决编程问题时,有时最简单的解决方案(如改变初始化值)往往是最有效的,而深入理解现有代码的逻辑是找到这些简单解决方案的关键。
以上就是优化Python电梯模拟:支持0层起始与完整楼层显示的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号