3

我需要 Python 中的电梯模拟器方面的帮助,但我没有太多经验。应该有一个用户输入的客户数量,这些客户具有随机的起始楼层和目的地楼层。现在我只是在编写电梯一直到顶部然后回到底部的简单策略。当我运行我的代码时,程序无限循环。我不知道为什么。另外,我不确定如何编写我的建筑物的输出方法,我想显示哪些客户有哪些楼层以及电梯访问了多少楼层。谢谢你的帮助。

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)
4

1 回答 1

3

你的问题出在这里:

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()

当您执行 lift.reg_list.append(customer) 时,您将客户重新附加到列表中,增加其大小(self.customers 也是对同一列表的引用),因此“self.customers 中的客户”循环永远。

让我们关注“cus_list”:

elevator = Elevator(floor_count, cus_list)
building = Building(floor_count, cus_list, elevator)

class Building(object):
    def __init__(self, num_of_floors, customer_list, elevator):
        self.total_floors = num_of_floors
        self.customers = customer_list

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 # <-------- THIS IS "cus_list" reference

最后在课堂建筑:

 elevator.reg_list.append(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 = []
于 2013-08-27T16:17:27.747 回答