I have the following code condition
if len(content_tags) >= 1 or tags_irrelevant == 'yes'\
and lengthproblem == 0\
and guess_language.guessLanguage(testlanguage) == 'en'\
and len(sentences) >= 3:
The problem is about the logic and the syntax. I want this to be evaluated as false no matter if if len(sentences) is not >= 3. But this is not happening. I think I might need some parenthesis somewhere or something. HELP!
andhas a higher precidence thanor, so theands are evaluated first, then theor, meaning that the logic you have described in text is not the logic you have described in code.If you want the first
orto be treated as a single case, then use brackets around it.That said, you haven’t given us a detailed explanation of the logical behaviour you want from this, so I’d suggest sitting down and working that out properly.
If you need to test your logic, then use a simple test function that prints out so you know what gets evaluated and when.
You can clearly see the first thing evaluated is the first
and, then it tries to evaluate the left hand side of theandand so gets theor. It tries to evaluate this, getsTruefor the first value, and so short-circuits, returningTrueto theand, which also short circuits, returningTrue(well, actually 1, butTruefor the purposes of this example). When the brackets are there, it is evaluated in the way you wanted.