Python中的电梯模拟器
我需要Python中的电梯模拟器的帮助,但我没有太多经验。应该有用户输入的具有随机起始楼层和目标楼层的客户数量。现在我正在编写一个简单的策略,电梯一路往上,然后再回到底部。当我运行代码时,程序会无限循环。我不明白为什么。此外,我不知道如何对我的建筑的输出方法进行编码,我想显示哪些客户有哪些楼层,电梯访问了多少楼层。谢谢你的帮助Python中的电梯模拟器,python,Python,我需要Python中的电梯模拟器的帮助,但我没有太多经验。应该有用户输入的具有随机起始楼层和目标楼层的客户数量。现在我正在编写一个简单的策略,电梯一路往上,然后再回到底部。当我运行代码时,程序会无限循环。我不明白为什么。此外,我不知道如何对我的建筑的输出方法进行编码,我想显示哪些客户有哪些楼层,电梯访问了多少楼层。谢谢你的帮助 import random class Elevator(object): def __init__(self, num_of_floors, register
import random
class Elevator(object):
def __init__(self, num_of_floors, register_list, direction = "up", cur_floor=1):
self.total_floors = num_of_floors
self.reg_list = register_list
self.floor = cur_floor
self.direct = direction
def move(self):
"""Moves the elevator one floor"""
if self.total_floors == self.floor:
self.direct = "down"
if self.direct == "up":
self.floor += 1
else:
self.floor -= 1
def register_customer(self, customer):
self.reg_list.append(customer)
def cancel_customer(self, customer):
self.reg_list.remove(customer)
class Building(object):
def __init__(self, num_of_floors, customer_list, elevator):
self.total_floors = num_of_floors
self.customers = customer_list
def run(self):
while elevator.floor != 0:
for customer in self.customers:
if elevator.floor == customer.on_floor:
elevator.reg_list.append(customer)
customer.indicator = 1
elif elevator.floor == customer.going_floor:
elevator.reg_list.remove(customer)
customer.indicator = 0
customer.fin = 1
elevator.move()
def output(self):
pass
class Customer(object):
def __init__(self, ID, num_of_floors, cur_floor=0, dst_floor=0, in_elevator=0, finished=0):
self.ident = ID
self.indicator = in_elevator
self.fin = finished
cur_floor = random.randint(1, num_of_floors)
self.on_floor = cur_floor
dst_floor = random.randint(1, num_of_floors)
while dst_floor == cur_floor:
dst_floor = random.randint(1, num_of_floors)
self.going_floor = dst_floor
customer_count = int(input("How many customers are in the building?: "))
floor_count = int(input("How many floors does the building have?: "))
cus_list = []
for i in range(1, customer_count+1):
cus_list.append(Customer(i, floor_count))
elevator = Elevator(floor_count, cus_list)
building = Building(floor_count, cus_list, elevator)
你的问题在于:
def run(self):
while elevator.floor != 0:
print(elevator.floor)
for customer in self.customers:
print(customer)
if elevator.floor == customer.on_floor:
elevator.reg_list.append(customer)
customer.indicator = 1
elif elevator.floor == customer.going_floor:
elevator.reg_list.remove(customer)
customer.indicator = 0
customer.fin = 1
elevator.move()
当您执行电梯.reg_list.append(客户)时,您将客户重新追加到列表中,并增加其大小(self.customers也是对该列表的引用),因此“self.customers中的for customer”将永远循环
下面是“客户列表”:
电梯是在FYI类范围之外创建的全局变量
修复可能如下所示
电梯一开始是空的,对吗
class Elevator(object):
def __init__(self, num_of_floors, register_list, direction = "up", cur_floor=1):
self.total_floors = num_of_floors
self.reg_list = []
我不想坐电梯:P检查一下你在大楼里的while环,看起来很有趣。。。尝试将电梯.floor变量输出到循环中的控制台。这
而循环对我来说很好-它表示客户将要去他/她所在楼层以外的楼层。很抱歉,编辑了该变量。只要楼层数足够大;)不,它们是对同一列表的两个不同引用。OP可能应该在电梯中使用self.reg\u list=[]
,这样电梯就不会完全启动。是的,我在上面的回答中提到了相同的列表。抱歉,时间太长了:我会继续展示电梯的功能吗?例如,使用打印语句显示“电梯在一楼接客户1……电梯移到二楼接客户3……电梯移到三楼,放下客户1,接客户4……等等”。例如:打印(“电梯接客户”+客户+“在楼层”+电梯.楼层)这是python 3语法。Python 2省略了“(”s
elevator.reg_list.append(customer)
class Elevator(object):
def __init__(self, num_of_floors, register_list, direction = "up", cur_floor=1):
self.total_floors = num_of_floors
self.reg_list = []