如果是Python中的else

发布于 2024-11-26 06:26:41 字数 890 浏览 1 评论 0原文

我有以下函数,当我调用它时,它会打印出混乱的结果,但是如果 if 条件为 false,那么它不会进入 else 分支,我做错了什么?

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' );

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' );

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

始终不够 2024-12-03 06:26:41

您可能会在这里遇到异常:

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

您应该添加 s:

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

(我假设 mess['commandName'] 是一个字符串)

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)

爱,才寂寞 2024-12-03 06:26:41

if 语句中的条件计算结果为 False 时,else 块肯定会被执行。是什么让你有不同的想法?

我怀疑您的代码引发了一个异常,您似乎在外部 try-except 块中忽略或沉默该异常。例如,该行将

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

引发 TypeError,因为您传递了 3 个参数,但只有 2 个占位符,因为您似乎忘记了“I receive % command”附近的“s”。

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

爱冒险 2024-12-03 06:26:41

如果 "self.reiseError" 实际上引发了错误,您将永远不会到达 isRunning[self.clientname] = False

顺便说一句,不需要在 if 语句中使用括号,就好像它是 C 语法派生语言一样。

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.

掌心的温暖 2024-12-03 06:26:41

它是否会转到 if 分支,但您的 print 只是没有从缓冲区中刷新?

另外,self.reiseError( error ) 对我来说看起来像是拼写错误,所以你应该在那里得到一个 AttributeError

粘贴到 Stack Overflow 后也可能出现不显示的缩进错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文