如果是Python中的else
我有以下函数,当我调用它时,它会打印出混乱的结果,但是如果 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能会在这里遇到异常:
您应该添加
s
:(我假设
mess['commandName']
是一个字符串)You probably get an exception here:
You should add
s
:(I assume
mess['commandName']
is a string)当
if
语句中的条件计算结果为False
时,else
块肯定会被执行。是什么让你有不同的想法?我怀疑您的代码引发了一个异常,您似乎在外部
try
-except
块中忽略或沉默该异常。例如,该行将引发
TypeError
,因为您传递了 3 个参数,但只有 2 个占位符,因为您似乎忘记了“I receive % command”附近的“s”。When the condition in your
if
statement evaluates toFalse
, theelse
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 linewill 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".如果
"self.reiseError"
实际上引发了错误,您将永远不会到达isRunning[self.clientname] = False
。顺便说一句,不需要在
if
语句中使用括号,就好像它是 C 语法派生语言一样。If
"self.reiseError"
actually raises an error, you'll never get toisRunning[self.clientname] = False
.By the way, there is no need to use parenthesis in
if
statements as if it was C-syntax-derived language.它是否会转到
if
分支,但您的print
只是没有从缓冲区中刷新?另外,
self.reiseError( error )
对我来说看起来像是拼写错误,所以你应该在那里得到一个AttributeError
。粘贴到 Stack Overflow 后也可能出现不显示的缩进错误。
Could it be going to the
if
branch but yourprint
just isn't getting flushed out of the buffer?Also,
self.reiseError( error )
looks like a misspelling to me, so you should get anAttributeError
there.An indentation error that doesn't show up after the paste to Stack Overflow is also possible.