user1978826
user1978826

Reputation: 203

Python statement always being executed assitance

I have the following the condition statement:

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

The problem is that the second and third clause work as expected,however, if the sc equals both and both fc and bc are false this statement still executes, and I don't know why.

Upvotes: 0

Views: 34

Answers (1)

User
User

Reputation: 14853

You wrote

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

I think you meant

#                                     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

You can and and or are weaker that any operation on numbers so this is also correct:

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

Upvotes: 1

Related Questions