我有一份家庭作业,真的是我的面条。它涉及电梯模拟,该模拟采用用户输入的楼层数和使用电梯的人数。人们的起始楼层和目的地楼层是楼层内的随机数字。
我意识到我的代码非常稀疏,并且存在相当多的空白,但我真的不知道从哪里开始。
我需要在构建类中提供帮助,例如如何使run()和output()部分工作。任何其他提示将非常感谢和有帮助。请注意,我不是在寻找有人为我做代码,而是要握住我的手并告诉我要走哪条路。对我来说,课程似乎完全不可思议。
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
答案 0 :(得分:6)
self
所有方法的参数。__init__
,
构造函数。self.varible
为你的成员变量。main
功能。return
来自函数或函数的值
方法global
个变量。答案 1 :(得分:3)
也许你的建筑类应该这样开始。
class building:
def __init__(self, floors, customers):
self.num_of_floors = floors
self.customer_list = customers
self.elevator = elevator()
答案 2 :(得分:1)
您应该在Python Tutorial或Dive into Python上花一些时间。
答案 3 :(得分:0)
每个方法的第一个参数是对象的引用,通常称为self。您需要它来引用对象的实例成员。
其次,从类中引用全局变量被认为是个坏主意。您可以通过构造函数或参数将它们更好地传递给类。