【问题标题】:TypeError: unsupported operand type(s) for +: 'int' and 'instance'类型错误:+ 不支持的操作数类型:“int”和“instance”
【发布时间】:2017-07-18 15:57:44
【问题描述】:

我对 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 行中。我只是用它来检查它是否还在工作一段时间。大脑被炸了,出于某种原因,我认为离开很重要。哎呀。

【问题讨论】:

  • 为什么你的distance 类存在?
  • 错误信息是什么?
  • @CaryShindell 在标题中
  • 需要堆栈跟踪来帮助(或有错误的行)
  • distance 函数可以有多个returns。由于您想将1 添加到其中,您必须确保它们都是数字的。正如@Rob 所说,删除class 定义。还有distance需要参数才能运行;不能这样称呼它distance()..

标签: python python-2.7 simpy


【解决方案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)。

【讨论】:

  • 谢谢,帮助我了解我做错了什么。我只是把它变成了一个函数,没有问题。也删除了 +1,这只是我不久前留下的一个健全性检查。
【解决方案2】:

这两行:

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

您创建一个 distance,其中包含一个方法,也称为distance。这几乎肯定不是你想要的。从你的方法的签名和它的使用我推断你正在尝试创建一个全局函数。

删除两行中的第一行,并将方法的缩进向左移动一步:

def distance(operator_location,local_location):
    ...

【讨论】:

  • 这行得通,我不知道为什么我想多了。谢谢。
【解决方案3】:

据我所知,确保所有相加的数字都是整数

total_walk=total_walk + distance() + 1

total_walk 和 distance() 加 1 时是整数吗?这也让我感到困惑。我现在可能像个白痴一样喋喋不休,但这是我对正在发生的事情的唯一想法。

【讨论】:

    猜你喜欢
    • 2018-09-01
    • 2013-10-04
    • 2021-06-16
    • 2014-06-16
    • 2014-03-16
    • 2020-08-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多