English 中文(简体)
甲型电梯模拟问题
原标题:Python elevator simulation problem
  • 时间:2009-12-07 22:16:20
  •  标签:
  • python
  • oop

我有一个家庭作业任务,这真的让我头疼。它涉及一个电梯模拟,需要用户输入楼层数和使用电梯的人数。人们的出发楼层和目的地楼层是楼层内的随机数字。

我认识到,我的法典非常稀少,存在一些差距,但我确实不知道从哪里走。

我需要在建筑类别内提供帮助,例如如何使(或)产出部分发挥作用。 任何其他建议都会得到高度赞赏和帮助。 请注意,我并不指望有人为我做法典,而是想把我的手交给我,告诉我什么路要走。 班级似乎对我完全是神秘的。

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.

或许你们的建筑阶层应该开始这样做。

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

每一种方法的第一参数都是参照标的,通常称为自称。 贵方需要参考对象。

第二,从一个类别内提及全球变量被认为是坏的。 你们可以通过构造或参数把他们带给一个班子。





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签