If statement Python语句总是在辅助状态下执行
我有以下条件声明:If statement Python语句总是在辅助状态下执行,if-statement,python-3.x,logic,If Statement,Python 3.x,Logic,我有以下条件声明: if (sc == 'Both') and (fc == 'True') or (bc == 'True'): do this if (sc == 'Front') and (fc == 'True'): do this if (sc == 'Back') and (bc == 'True'): do this 问题是第二和第三个子句按预期工作,但是,如果sc等于两者,fc和bc都为false,则此语句仍会执行,我不知道为什么。您写道 if ((
if (sc == 'Both') and (fc == 'True') or (bc == 'True'):
do this
if (sc == 'Front') and (fc == 'True'):
do this
if (sc == 'Back') and (bc == 'True'):
do this
问题是第二和第三个子句按预期工作,但是,如果sc等于两者,fc和bc都为false,则此语句仍会执行,我不知道为什么。您写道
if ((sc == 'Both') and (fc == 'True')) or (bc == 'True'):
do this
if (sc == 'Front') and (fc == 'True'):
do this
if (sc == 'Back') and (bc == 'True'):
do this
我想你是说
# or binds weaker than and so it needs brackets
if (sc == 'Both') and ((fc == 'True') or (bc == 'True')):
do this
if (sc == 'Front') and (fc == 'True'):
do this
if (sc == 'Back') and (bc == 'True'):
do this
您可以将和
和或
与对数字进行的任何操作相比较弱,因此这也是正确的:
if sc == 'Both' and (fc == 'True' or bc == 'True'):
do this
if sc == 'Front' and fc == 'True':
do this
if sc == 'Back' and bc == 'True':
do this
Python解释器不同意。我怀疑你遗漏了什么<代码>>>>('Both'='Both')和('False'='True')或('False'='True')
False
等待。。。您使用的是字符串('False'
)还是布尔值(False
)?这里的python不这样做。请详细说明您是如何设置这些值的。我认为如果这些值是sc=“Both”,fc=bc=“False”,那么您的第一个示例也不应该包含在if子句中。