0

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 but I received % command from %s " % self.lastCommand, mess['commandName'], self.clientname
            self.reiseError( error )
            isRunning[self.clientname] = False
            print( 'else' );
7
  • 1
    It seems valid. The only weird thing I see is reiseError(). Shouldn't that be raiseError() ? Commented Jul 25, 2011 at 8:17
  • Your else condition is probably connected with your inner if. Check your indentation. Commented Jul 25, 2011 at 8:19
  • @Paolo: I guess you are right. Identation is 8 first, then it becomes 4 in the inner level. Commented Jul 25, 2011 at 8:21
  • 1
    How do you know that the condition is false? Commented Jul 25, 2011 at 8:21
  • From the message, I printed out, and the if statement will be false. Commented Jul 25, 2011 at 8:27

4 Answers 4

3

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)

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, it's a string, but I'm not getting this error, because the program not going in the else branch.
Just to make it clear, have you debugged it and see that it doesn't go the the else branch or you only assume?
2

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".

Comments

2

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.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.