我有以下条件语句:
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 都为假,则该语句仍然执行,我不知道为什么。
我有以下条件语句:
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 都为假,则该语句仍然执行,我不知道为什么。
你写了
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
您可以and
并且or
比对数字的任何操作更弱,所以这也是正确的:
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