如果...... Elif ......其他流程

时间:2012-07-30 08:04:31

标签: python flow

我创建了一个带有if流量控件的函数,我想知道一些关于它的事情。

class Supplier(Process):

    def weeklySupply(self):
        global D1Vals
        xMeet = D1Vals[0]
        lead = 0
        capac = 150.0
        supply = 0
        if stocked.amount >= 15000:
            supply = 0
            yield put, self, stocked, supply
            S1Vals.append(0)
        elif supply >= capac:
            supply = capac
            yield put, self, stocked, supply
            S1Vals.append(capac)
        else:
            supply = random.triangular(.70 * xMeet , xMeet , xMeet * 1.05)
            yield put, self, stocked, supply
            print ('Week:'+'%6.0f:Supplied: %6.0f units. CurSupply = %6.0f' %
                   (now() + 1., supply, stocked.amount))
            yield hold, self, lead
            S1Vals.append(supply)
            del D1Vals[0]

我编码的是:

  • 如果库存对象中的金额>> 15000,则该对象生成零供应 或者,如果产生的供应值大于预定义的“容量”,则供应值='容量'。

  • 如果上述情况不属实,则将供应值作为随机分布生成。当我得到我的输出时,供应值确实超过150的“容量”值。

我是否误解了如何进行流程?

2 个答案:

答案 0 :(得分:1)

不要像你在问题中那样将if elif构造与某种or混淆。仅当elif条件为if时,才会评估False条件。 我想,你真正想要的是:

if stocked.amount >= 15000:
    if supply >= capac:
        supply = capac
        yield put, self, stocked, supply
        S1Vals.append(capac)
     else:
        supply = 0
        yield put, self, stocked, supply
        S1Vals.append(0)
else:
    supply = random.triangular(.70 * xMeet , xMeet , xMeet * 1.05)
    ...

但是,如果supply在函数开头没有初始化为0,那么这只会起作用。

答案 1 :(得分:0)

似乎是一种享受,以及一些让我思考的好东西。我能看到的唯一问题是现在我的S1Vals列表已停止将零电源附加到列表中。我可以在输出图表上看到这个,也可以在我做一个len(S1Vals)时看到这个。最终的代码是您的建议:

if stocked.amount >= 15000:
        if supply >= capac:
            supply = capac
            S1Vals.append(capac)
            yield put, self, stocked, supply
        else: 
            supply = 0.0
            yield put, self, stocked, supply
            S1Vals.append(supply)
    else:
        supply = min(capac, random.triangular(.70 * xMeet , xMeet , xMeet * 1.05))
        yield put, self, stocked, supply
        print ('Week:'+'%6.0f:Supplied: %6.0f units. CurSupply = %6.0f' %
               (now() + 1., supply, stocked.amount))
        yield hold, self, lead
        S1Vals.append(supply)
        del D1Vals[0]

为了到达此处,我使用了MaxPower上面的流量建议,也是min(supply,capac),并声明了global supply。最后一个问题似乎supply = 0.0从未开始,但它应该真的吗?