elif声明从未执行过

时间:2015-06-01 16:54:43

标签: python control-flow

if IsInBrew() == 'InBrew':
    # first lets work out where we are...
    thisBrew = getCurrentBrewID()
    latestStage = getCurrentStage(thisBrew)

    elapsedtime = now - latestStage[1]
    hours = divmod(elapsedtime.total_seconds(),3600)
    minutes = divmod(hours[1], 60)

    totalMinutes = (hours * 60) + minutes

    #addDebug('%d hours, %d minutes' % (hours[0], minutes[0]))

    if latestStage[0] == 'Fill HLT':
        # we can't do anything until HLT is full
        addDebug('Current Temp:' + str(getHLTTemperature()))
    elif latestStage[0] == 'Boil HLT':
        # we are now boiling up so check temp against our max value
        addDebug('In Boil HLT %s' % getHLTTemperature())
        MaxHLT = float(getKeyValue(thisBrew, 'HLTReleaseTemp'))
        addDebug('Current: %s Max: %s' % (getHLTTemperature(), MaxHLT))
        if float(getHLTTemperature()) >= MaxHLT:
            # switch off the boil and release the water
            GPIO.digitalWrite(HLTBOIL, GPIO.HIGH)
            GPIO.digitalWrite(HLTRELEASE, GPIO.HIGH)
            moveToNextStage = True
    elif latestStage[0] == 'Maintain Mash':     
        currentMashTemp = float(getMashTemperature())
        print currentMashTemp
        MaintainMashTemp = float(getKeyValue(thisBrew, 'MashTempMaintain'))

        print 'Current Mash %.2f Maintain Temp %.2f' % (currentMashTemp, MaintainMashTemp)

        #if Mash Temp too hot turn off boil
        if currentMashTemp > (MaintainMashTemp + TOLERANCE):
            print 'Mash temp high, turn off boil'
            GPIO.digitalWrite(MASHMAINTAIN, GPIO.LOW)

        #if Mash Temp too low turn on boil
        if currentMashTemp < MaintainMashTemp:
            print 'Mash temp low, turn on boil'

            GPIO.digitalWrite(MASHMAINTAIN, GPIO.HIGH)

        # release if we are at temp and elapsed time = maintain duration    
        if  totalMinutes >= int(getKeyValue(thisBrew, 'MashDuration')) and currentMashTemp >= MaintainMashTemp:
            GPIO.digitalWrite(MASHMAINTAIN, GPIO.LOW)
            GPIO.digitalWrite(MASHRELEASE, GPIO.HIGH)
            moveToNextStage = True

print 'Latest Stage is %s' % latestStage[0] 

if latestStage[0] == 'Maintain Mash':
    print 'Equal'

所以我有这个奇怪的问题,我的第二个elif(elif latestStage[0] == 'Maintain Mash':)从未在其中运行代码。第一个if和elif工作正常。

我甚至在底部添加了一张支票,因此我可以看到“等于”#39;正在输出。我不能为我的生活看到为什么第二个elif永远不会执行。

getCurrentStage(thisBrew)函数将返回一个字符串和一个日期时间值,因此是latestStage [0],它是字符串,而latestStage [1]是上次更新的日期。

1 个答案:

答案 0 :(得分:0)

这是缩进!

我不是新手编码器,但我是一个蟒蛇新手。我知道缩进很重要但是当我复制代码并使用linux文本编辑器(nano)时,我可以看到空格和制表符的混合。所以我检查了我在Sublime中使用的设置,并确保所有内容都缩进为4个空格并且瞧!

感谢大家的帮助!