Python电梯仿真问题
我有一个家庭作业,真的很烦人。它涉及一个电梯模拟,该模拟采用用户输入的楼层数和使用电梯的人数。人员的起始楼层和目标楼层是楼层内的随机数 我意识到我的代码非常稀疏,有相当多的差距,但我真的不知道从这里走到哪里 我需要building类中的帮助,例如如何使run()和output()部分工作。任何其他提示都将不胜感激和帮助。请注意,我不是在找人帮我做代码,而是握着我的手告诉我该走哪条路。课堂对我来说似乎完全是个谜Python电梯仿真问题,python,oop,Python,Oop,我有一个家庭作业,真的很烦人。它涉及一个电梯模拟,该模拟采用用户输入的楼层数和使用电梯的人数。人员的起始楼层和目标楼层是楼层内的随机数 我意识到我的代码非常稀疏,有相当多的差距,但我真的不知道从这里走到哪里 我需要building类中的帮助,例如如何使run()和output()部分工作。任何其他提示都将不胜感激和帮助。请注意,我不是在找人帮我做代码,而是握着我的手告诉我该走哪条路。课堂对我来说似乎完全是个谜 import random floors=raw_input('Please ent
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
随机导入
楼层=原始输入('请输入模拟的楼层数:')
而floors.isalpha()或floors.isspace()或int(floors)
- 你需要了解自己的
self
参数设置为所有方法
- 您需要了解
\uuuuu init\uuuuu
,
构造器
- 您需要了解
self.varible
对于您的成员变量
- 您需要了解如何设置
main
功能
- 你需要了解如何
返回函数或函数的值
方法
- 您需要了解如何从函数或方法中分配给
全局
变量
也许你的建筑课应该这样开始
class building:
def __init__(self, floors, customers):
self.num_of_floors = floors
self.customer_list = customers
self.elevator = elevator()
您应该明确地在或上花费一些时间。每个方法的第一个参数都是对对象的引用,通常称为self。您需要它来引用对象的instancemembers
其次,从类内部引用全局变量被认为是一个坏主意。您可以通过构造函数或参数更好地将它们传递给类。+1。研究这些东西并理解这些概念应该对你有很大帮助。是的,我在阅读代码时的第一个想法是“哦,哇……从哪里开始?”回头看课程笔记、课本等可能也是个好主意。+1。此外,您还应该从小处开始逐步构建:首先设置一个“hello world”main
函数。然后尝试实现一个简单的类,该类从\uuuu init\uuuu
打印“hello world”,然后。。。继续增量工作!只有当人们在电梯已经很忙的时候“出现”在地板上时,电梯模拟才有意思。如果他们从一开始就在那里,那么问题就太简单了,没有代表性。。。。电梯也应该有最大的使用量@帕斯卡,如果他们没有正确地上过课,看起来就像是很难的家庭作业yet@gnibbler:在“适当覆盖”和“适当覆盖”之间有区别但是,是的,这可能是第一阶段,“学习什么是类和对象”,在以后的阶段,他们会将其增强为更有趣的内容。你能发布作业中的文本,以便我们了解其要求吗?你们是最好的。我意识到代码完全是垃圾,但有了这一点知识,我将开始重新编写整个代码