开发者

If else in Python

开发者 https://www.devze.com 2023-03-22 12:00 出处:网络
I have the following function, when I call it\'s printing out the mess but if the if condition is false than it\'s not going the else branch, what I\'m doing wrong?

I have the following function, when I call it's printing out the mess but if the if condition is false than it's not going the else branch, what I'm doing wrong?

def lidarMessageHandler( self, mess ):
        print( mess );
        #Check if I received the right command
        if( COMMANDTABLE[commandList[self.clientname]['lastcommand']]['id'] == mess['commandName'] ):
            print( 'if' )
            #Check if it's a blocking command            
            commandList[self.clientname]['isready'] = True
            if( self.start ):
                self.waitingForSettingsHandler( mess )
                return                           
        else:
            error = "I waited the answer for the following command %s b开发者_JAVA技巧ut I received % command from %s " % self.lastCommand, mess['commandName'], self.clientname
            self.reiseError( error )
            isRunning[self.clientname] = False
            print( 'else' );


You probably get an exception here:

error = "I waited the answer for the following command %s but I received % command from %s " % self.lastCommand, mess['commandName'], self.clientname
                                                                         ^

You should add s:

error = "I waited the answer for the following command %s but I received %s command from %s " % self.lastCommand, mess['commandName'], self.clientname
                                                                         ^

(I assume mess['commandName'] is a string)


When the condition in your if statement evaluates to False, the else block is most certainly executed. What makes you think otherwise?

I suspect that your code raises an exception that you seem to ignore or silence in an outer try-except block. For example, the line

error = "I waited the answer for the following command %s but I received % command from %s " % self.lastCommand, mess['commandName'], self.clientname

will raise a TypeError, since you are passing 3 arguments but only have 2 placeholders, as you seem to have forgotten the "s" near "I received % command".


If "self.reiseError" actually raises an error, you'll never get to isRunning[self.clientname] = False.

By the way, there is no need to use parenthesis in if statements as if it was C-syntax-derived language.


Could it be going to the if branch but your print just isn't getting flushed out of the buffer?

Also, self.reiseError( error ) looks like a misspelling to me, so you should get an AttributeError there.

An indentation error that doesn't show up after the paste to Stack Overflow is also possible.

0

精彩评论

暂无评论...
验证码 换一张
取 消