elif语句从未执行

2024-05-15 15:44:25 发布

您现在位置:Python中文网/ 问答频道 /正文

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工作正常。你知道吗

我甚至在底部添加了一个复选框,这样就可以看到“Equal”被输出。我一辈子都不明白为什么第二个elif永远不会执行。你知道吗

getCurrentStage(thisBrew)函数将返回字符串和日期时间值,因此latestStage[0]是字符串,latestStage[1]是最后一次更新的日期。你知道吗


Tags: gpioifweprintmashelifdigitalwritehours
1条回答
网友
1楼 · 发布于 2024-05-15 15:44:25

是压痕!你知道吗

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

谢谢大家的帮助!你知道吗

相关问题 更多 >