Simple Elevator Program Python Using While For Loop. Why does my loop not update every item on list? -
my below loop in python goes through list of customers seems once removes customer list go start of while loop instead of repeating through rest of customers. can please have @ doing wrong? entire code below , attached.
author = 'alan doonan' import random import time class building: # defines class building number_of_floors = 0 # sets number_of_floors variable 0 customer_list = [] # creates empty array customer_list elevator = 0 # sets elevator variable 0 def __init__(self, floors, customers): # initialize building self.number_of_floors = floors # assigns floors entered number_of_floors customerid in range(1, customers + 1): # assigns number of customers entered customer_list in order new = customer(customerid,self.number_of_floors) # creates instance called new of customer class number of customers entered in input self.customer_list.append(new) # appends new instance of customer customer_list self.customer_list.sort(key = lambda x: x.current_floor) # sorts customer_list current_floor customer on # prints self.elevator = elevator(floors,self.customer_list) # creates instance of elevator inputted floors , assigns customer_list register_list # prints self.run() # runs run method below def run(self): # method operate elevator print('++++++++++++++++++++++++++elevator starting+++++++++++++++') # prints print('there %d customers in building' % (len(self.customer_list))) # prints number_of_customers = len(self.customer_list) # assigns current number of customers number_of_customers variable self.output() # runs output method below def output(self): customer in self.customer_list: #prints lists of customers in building , details print("customer",customer.customerid,"is on floor",customer.current_floor,"and wants go to",customer.destination_floor) #elevator moving loop while (self.elevator.current_floor < self.elevator.number_of_floors): self.elevator.current_floor +=1 print('elevator moving up') print(len(self.customer_list),'customers in lift.') print('++++++++++++++++++++++++++++++++++++++++++++++++++++') print('floor',self.elevator.current_floor) customer in self.customer_list: # loop each instance of custumer in customer_list if (self.elevator.current_floor == customer.current_floor) & customer.customer_direction == 1: customer.in_elevator = true print('customer',customer.customerid,'has entered lift') if (self.elevator.current_floor == customer.destination_floor) & (customer.in_elevator == true) & customer.customer_direction ==1: customer.in_elevator = false self.customer_list.remove(customer) print(customer.customerid,'has reached destination') #elevator moving down loop while (self.elevator.current_floor <= self.number_of_floors) & (self.elevator.current_floor > 1): self.elevator.current_floor -= 1 print(len(self.customer_list),'customers in lift.') print('elevator moving down') print('++++++++++++++++++++++++++++++++++++++++++++++++++++') print('floor',self.elevator.current_floor) customer in self.customer_list: if (customer.in_elevator == true): customer.current_floor = self.elevator.current_floor if (self.elevator.current_floor == customer.destination_floor) & (customer.in_elevator == true) & (customer.customer_direction == -1): customer.in_elevator = false self.customer_list.remove(customer) print('customer',customer.customerid,'has reached destination') print('there are',len(self.customer_list),'trapped in elevator') #prints print('there are',len(elevator.register_list),'people left on register') print('elevator run done!!!') #prints print('customers stuck in lift below') stuck in self.customer_list: print('cust. id:',stuck.customerid,'dest. floor:',stuck.destination_floor,'curr. floor:',stuck.current_floor,'in elevator',stuck.in_elevator,'direction',stuck.customer_direction) class elevator: number_of_floors = 0 # number of floors register_list = [] # list of customers in elevator current_floor = 0 # current floor of elevator = 1 # moves elevator down = -1 # moves elevator down def __init__(self, number_of_floors, register_list): self.number_of_floors = number_of_floors self.register_list = register_list def move(self): # method move elevator 1 floor pass; def register_customer(self, customers): # customer goes elevator reg in customers: self.register_list.append(reg) def cancel_customer(self, customers): # customer goes out of elevator pass; class customer: current_floor = 0 # current floor of elevator destination_floor = 0 # destination floor of elevator customerid = 0 # customers id in_elevator = false # denotes whether customer in elevator finished = false # denotes whether customer has reached destination floor customer_direction = 0 def __init__(self, customerid, floors): # initilize customer class self.customerid = customerid # assigns self.customerid customerid self.current_floor = random.randint(1, floors) # assigns self.current_floor random int between 1 , floors entered self.destination_floor = random.randint(1, floors) # assigns seslf.destination_floor random int between 1 , floors entered while self.destination_floor == self.current_floor: self.destination_floor = random.randint(1, floors) if self.current_floor < self.destination_floor: self.customer_direction = 1 else: self.customer_direction = -1 def header(): # elevator animation @ beginning of program print(" elevator opening ") time.sleep(.2) print("+++++++++++++++++++++++++++++||+++++++++++++++++++++++++++++++++++") time.sleep(.2) print("+++++++++++++++++++++++++| |++++++++++++++++++++++++++++++++") time.sleep(.2) print("++++++++++++++++| |+++++++++++++++++++") time.sleep(.2) print("++++++| |+++++++++") time.sleep(.2) print(" ") time.sleep(.2) print(" elevator closing ") time.sleep(.2) print("++++++| |+++++++++") time.sleep(.2) print("++++++++++++++++| |+++++++++++++++++++") time.sleep(.2) print("+++++++++++++++++++++++++| |++++++++++++++++++++++++++++++++") time.sleep(.2) print("+++++++++++++++++++++++++++++||+++++++++++++++++++++++++++++++++++") def main(): # main method try: # try/except user input menu floors = int(input('enter number of floors: ')) # enter floors , assign floors customers = int(input('enter number of customers: ')) # enter customers , assign customers building = building(floors, customers) # instance of building created inputs of floors , customers # create instance of building class (building) except valueerror: print('you didnt enter number. start again.') main() if __name__ == "__main__": # header() main()
it hard answer question without providing more scope. please more specific. without more exact question, cannot sure-so please provide examples of input , output lead believe this. however, if looks starting @ beginning of while loop because keeps printing out same information, may have how declared classes; have overloaded customerid both class variable , instance variable. more information see docs. https://docs.python.org/2/tutorial/classes.html#class-objects basically, rid of class variables inside customer instance variables , refactor sections below comments #elevator moving loop , going down loop consolidate them, since reusing lot of code needlessly here. refine question asking clearer
Comments
Post a Comment