2017-07-18 84 views
0

我對Python並不擅長,我知道某些東西與我的類混爲一談,但我不確定它出了什麼問題。這似乎是一個相當普遍的問題,但無論出於什麼原因,我都很難理解爲什麼。TypeError:不支持的操作數類型爲+:'int'和'instance'

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線。我只是用它來檢查它是否在一段時間後才工作。大腦被炸,出於某種原因,我認爲這是足夠重要的離開。哎呀。

+0

爲什麼你的「距離」類存在? – user2357112

+0

什麼是錯誤信息? –

+0

@CaryShindell標題 –

回答

0

首先,您的距離等級沒有__init__方法,這是一個問題。然後,由於您正在嘗試添加distance()+1,但您正在嘗試添加創建距離類的實例,而不是像您打算的那樣實際調用其中的函數。您需要指定一個變量,如d = distance(),並執行d.distance(operator_location, global_location)+1或簡單地distance().distance(operator_location, global_location)+1

此外,它似乎並不真正使用距離作爲一個類,而是打算做一個全局函數,所以你也可以擺脫class distance行,而不必處理所有的類實例的東西(你只會做distance(operator_location, global_location)+1)。

+1

謝謝,幫助我瞭解我做錯了什麼。我只是把它變成了一個功能,沒有問題。還取消了+1,這只是一個完整的檢查,一會兒我就離開了。 – user3826299

1

在這兩條線:

class distance: 
    def distance(operator_location,local_location): 
     ... 

您創建一個distance其中包含方法,也叫distance。這幾乎肯定不是你想要的。從你的方法的簽名和它的使用我推斷你正在嘗試創建一個全局函數。

刪除第一個兩行,並移動方法的縮進一步向左:

def distance(operator_location,local_location): 
    ... 
+0

這個工作,我不知道爲什麼我推翻了它。謝謝。 – user3826299

0

從我可以告訴確保將所有的數字都是整數

total_walk=total_walk + distance() + 1 

是total_walk和distance()之間的一個整數嗎?這也讓我感到困惑。我現在可能喋喋不休,但這是我唯一的想法。

相關問題