我对Python不太好,我知道有些东西搞砸了我的班级,但我不确定它出了什么问题。这似乎是一个相当普遍的问题,但无论出于何种原因,我很难理解为什么。
class distance:
def distance(operator_location,local_location):
global hsff_conveyor
global hsff_unload
global hsdr_conveyor
global hsdr_unload1
global hsdr_unload2
global distance_between_hsff_load_and_hsff_unload
global distance_between_hsff_load_and_hsdr_load
global distance_between_hsff_load_and_hsdr_unload1
global distance_between_hsff_load_and_hsdr_unload2
global distance_between_hsff_and_hsdr_conveyor
global distance_between_hsff_unload_and_hsdr_unload1
global distance_between_hsff_load_and_hsdr_unload2
global distance_between_hsdr_load_and_hsdr_unload1
global distance_between_hsdr_load_and_hsdzr_unload2
global distance_between_hsdr_unload1_and_unload2
if operator_location==hsff_conveyor and local_location==hsff_unload:
return distance_between_hsff_load_and_hsff_unload
elif operator_location==hsff_conveyor and local_location==hsdr_conveyor:
return distance_between_hsff_load_and_hsdr_load
elif operator_location==hsff_conveyor and local_location==hsdr_unload1:
return distance_between_hsff_load_and_hsdr_unload1
elif operator_location==hsff_conveyor and local_location==hsdr_unload2:
return distance_between_hsff_load_and_hsdr_unload2
elif operator_location==hsff_unload and local_location==hsdr_conveyor:
return distance_between_hsff_and_hsdr_conveyor
elif operator_location==hsff_unload and local_location==hsdr_unload1:
return distance_between_hsff_unload_and_hsdr_unload1
elif operator_location==hsff_unload and local_location==hsdr_unload2:
return distance_between_hsff_unload_and_hsdr_unload2
elif operator_location==hsdr_conveyor and local_location==hsdr_unload1:
return distance_between_hsdr_load_and_hsdr_unload1
elif operator_location==hsdr_conveyor and local_location==hsdr_unload2:
return distance_between_hsdr_load_and_hsdr_unload2
elif operator_location==hsdr_unload1 and local_location==hsdr_unload2:
return distance_between_hsdr_unload1_and_unload2
else:
return int(0)
当它到达此处时,它会返回标题中的错误:
def hsff_fill_conveyor(env, operator, logfile):
global hsff_pick_and_load_time
global conveyor
global hsff_conveyor_holds
global operator_location
global total_walk
global total_walk_time
global hsff_conveyor
global hsff_unload
local_location=hsff_conveyor
while True:
if operator_location==hsff_conveyor:
hsff_start_loading_conveyor=env.now
yield hsff_raw_container_cont.get(hsff_pick_quantity)
hsff_conveyor_short_time=env.now-hsff_start_loading_conveyor
with operator.request() as req1:
yield req1
hsff_conveyor_waiting_for_operator=env.now-hsff_start_loading_conveyor
yield env.timeout(hsff_pick_and_load_time)
hsff_load_cycle_ende=env.now
yield hsff_conveyor_cont.put(hsff_pick_quantity)
elif operator_location!=hsff_conveyor:
hsff_operator_ready_to_walk=env.now
hsff_operator_ready_to_walk_short_time=env.now-hsff_operator_ready_to_walk
with operator.request() as req1:
yield req1
hsff_conveyor_waiting_for_operator=env.now-hsff_operator_ready_to_walk
yield env.timeout(20) #FILLER
walk_end=env.now
total_walk=total_walk + distance() + 1
operator_location=hsff_conveyor
total_walk = total_walk + distance() + 1
会引发错误。我也在其他方面发生这种情况。试图模拟一个有五种不同资源可以请求他的操作员。
编辑:+1并不是假设在total_walk行中。我只是用它来检查它是否还能工作一段时间。大脑被炒了,出于某种原因,我觉得离开是很重要的。 OPPS。
答案 0 :(得分:1)
在这两行中:
class distance:
def distance(operator_location,local_location):
...
您创建了一个类 distance
,其中包含方法,也称为distance
。这几乎肯定不是你想要的。根据您的方法签名及其使用,我推断您正在尝试创建一个全局函数。
删除两行中的第一行,并将方法的缩进向左移动一步:
def distance(operator_location,local_location):
...
答案 1 :(得分:1)
首先,您的距离类没有__init__
方法,这是一个问题。然后,您收到错误是因为您正在尝试添加distance()+1
,但执行distance()
会创建距离类的实例,而不是实际调用其中的函数。您需要指定一个变量,例如d = distance()
和d.distance(operator_location, global_location)+1
或distance().distance(operator_location, global_location)+1
。
此外,似乎你并没有真正使用距离作为一个类,而是打算创建一个全局函数,所以你也可以摆脱class distance
行,而不必处理所有类实例的东西(你只需做distance(operator_location, global_location)+1
)。
答案 2 :(得分:0)
据我所知,确保添加的所有数字都是整数
total_walk=total_walk + distance() + 1
添加1时,total_walk和distance()是否为整数?这对我来说也很困惑。我现在可能像个白痴一样bab呀学语,但这是我唯一的想法。