Hi, I would be grateful for some help with this Python problem. Your task is to
ID: 3801454 • Letter: H
Question
Hi, I would be grateful for some help with this Python problem.
Your task is to implement the simple elevator in Python using classes. The default strategy is the simple "start at the bottom, go to the top, then go to the bottom". Can you write a better strategy, one that is more efficient?
Description / Specification
Create three classes: Building, Elevator, and Customer.
Equip the building with an elevator. Ask user to customize the number of floors and the number of customers.
Program should have error checking to make sure the user inputs are valid. For example, if a user gives non-integer inputs, notify the user that the inputs are incorrect and prompt again.
Each customer starts from a random floor, and has a random destination floor.
Each customer will use the elevator only once, i.e., when a customer moves out of the elevator, he/she will never use it again.
When all customers have reached their destination floor, the simulation is finished.
Part of the grade on this assignment will be the appropriateness of your classes, methods, and any functions you use. The quality of the code will matter as well as the performance.
All class methods require a docstring for a general description of the method.
Implement both your own strategy and the default strategy and compare. Your strategy does not have to be better but the comparison is required.
Don't use any global variables.
Notes and Hints
In your main function, at the beginning ask the user for the number of floors and the number of customers which can be used to create an instance of Building. Then we only need to call the run and output methods repeatedly in a while loop.
Randomly select the floors (to and from) for each customer. Use the randint function from the random module. To compare efficiency of strategy, count the number of floors visited for your strategy versus the default strategy.
Make a demo to create usable output from your simulation.
Explanation / Answer
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)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.