开发者

Python elevator simulation problem

I have a homework assignment that's really baking my noodle. It involves an elevator simulation that takes user inputs for the number of floors and the number of people using the elevator. the people's starting floor and destination floors are random numbers within the floors.

I realize that my code is very sparse and that there's quite a few gaps, but I really don't know where to go from here.

I need help within the building class, such as how to make the run() and output() sections work. any other tips would be greatly appreciated and helpful. Note that i am not looking for someone to do the code for me, but to kind of hold my hand and tell me which way to go. Classes seem to be completely mystifying to me.

import random

floors=raw_input('Please enter the number of floors for the simulation:')  
while floors.isalpha() or floors.isspace() or int(floors) <=0:  
    floors=raw_input('Please re enter a digit for number of floors:')  
customers=raw_input('Please enter the number of customers in the building:')  
while customers.isalpha() 开发者_如何学编程or customers.isspace() or int(customers) <0:  
    customers=raw_input('Please re enter a digit for number of customers:')  
count = 1  

class building:  
    def num_of_floors():    
        num_of_floors = floors      
    def customer_list():    
        customer_list = customers    
    def run(self):    
    def output(self):    
        print elevator.cur_floor    

class elevator:    
    def num_of_floors():    
        building.num_of_floors    
    def register_list():    
        register_list = []    
    def cur_floor(building):    
        cur_floor = 1    
    def direction(self):    
        if elevator.cur_floor == 1:    
            direction = up    
        if elevator.cur_floor == floors:    
            direction = down    
    def move(self):    
        if elevator.direction == up:    
            cur_floor +=1    
        if elevator.direction == down:    
            cur_floor -=1    
    def register_customer(self, customer):    
        register_list.append(customer.ID)    
    def cancel_customer (self, customer):    
        register_list.remove(customer.ID)    

class customer:    
    def cur_floor(customer):    
        cur_floor = random.randint(0,int(floors))    
    def dst_floor(customer):    
        dst_floor = random.randint(0,int(floors))    
        while dst_floor == cur_floor:    
            dst_floor = random.randint(0,int(floors))    
    def ID():    
        cust_id = count    
        count+=1    
    def cust_dict(cust_id,dst_floor):    
        cust_dict = {cust_id:dst_floor}    
    def in_elevator():    
        in_elevator = 0    
        if customer.ID in register_list:    
            in_elevator = 1    
    def finished():    
        if customer.ID not in register_list:    
            pass    


  • You need to understand the self parameter to all methods.
  • You need to understand __init__, the constructor.
  • You need to understand self.varible for your member variables.
  • You need to understand how to setup a main function.
  • You need to understand how to return a value from a function or method.
  • You need to understand how to assign to global variables from within a function or method.


Maybe your building class should start like this.

class building:  
    def __init__(self, floors, customers):    
        self.num_of_floors = floors      
        self.customer_list = customers
        self.elevator = elevator()


You should definately spend some time on Python Tutorial or Dive into Python.


The first parameter of every method is a reference to the object and is usually called self. You need it to reference instancemembers of an object.

Second, referencing global variables from inside a class is considered a bad idea. You can better pass them to a class via the constructor or parameters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜