Python电梯仿真问题
我有一个作业,真让我头疼。这个作业是一个电梯模拟程序,需要用户输入楼层数和使用电梯的人数。每个人的起始楼层和目的楼层都是在这些楼层中随机生成的。
我意识到我的代码很简单,而且还有很多地方不完善,但我真的不知道接下来该怎么做。
我需要在建筑类(building class)里得到一些帮助,比如怎么让run()和output()这两个部分正常工作。其他的建议也非常欢迎和有帮助。请注意,我并不是想让别人帮我写代码,而是希望有人能指导我,告诉我该怎么做。对于类(classes)这个概念,我感觉完全搞不懂。
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
4 个回答
3
也许你的构建类应该这样开始。
class building:
def __init__(self, floors, customers):
self.num_of_floors = floors
self.customer_list = customers
self.elevator = elevator()
7
你需要了解所有方法中的 self
参数。
你需要明白 __init__
,也就是构造函数。
你需要理解 self.varible
这个用法,它是用来表示你的成员变量的。
你需要知道怎么设置一个 main
函数。
你需要学会如何从一个函数或方法中 return
(返回)一个值。
你还需要懂得如何在函数或方法内部给 global
变量赋值。