2

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    
6
  • An elevator simulation is only interesting if people "appear" on the floors while the elevator is already busy. If they are all there from the beginning it makes the problem too simple and unrepresentative. Commented Dec 7, 2009 at 22:25
  • ... and elevators should probably have a maximum occupancy too. ;) Commented Dec 7, 2009 at 22:28
  • @Pascal, seems like hard homework if they haven't covered classes properly yet Commented Dec 7, 2009 at 22:29
  • @gnibbler: there is a difference between "properly covered" and "properly grokked" ;) but, yes, this may be phase 1, "learn what classes and objects are", and they'll enhance this into something more interesting in later phases. Commented Dec 7, 2009 at 22:31
  • Could you post the text from your homework assignment so that we understand its requirements? Commented Dec 8, 2009 at 1:34

4 Answers 4

7
  • 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.
Sign up to request clarification or add additional context in comments.

3 Comments

+1. Looking into these things and understanding these concepts should go a long way in helping you.
Yeah, my first thought on reading through the code was "oh, wow... where to begin?" Going back to course notes, textbooks, etc would probably be a good idea too.
+1. Also you should start small and build incrementally: First setup a "hello world" main function. Then try implementing a simply class that prints "hello world" from __init__, then ... keep working incrementally!
3

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

1 Comment

you guys are the best. i realize that code is complete slop, but with this little amount of knowledge im going to start reworking the whole thing
1

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.