-2

Why can you not use the if statements with parenthesis to separate the two or options as shown below? Is there some rule or intricacy of the python program that I am neglecting?

if mass >= 2.6 and (sodium or nitrogen) <= 5:
PM 77-1
  • 12,933
  • 21
  • 68
  • 111
  • `(sodium or nitrogen) <= 5` doesn't make a lot of sense... it could test sodium it non zero else nitrogen against 5 but that's a very cryptic way of doing it. – Jean-François Fabre Sep 23 '21 at 21:31
  • `sodium or nitrogen` evaluates to `sodium` if `sodium` is truthy, else it will evaluate to `nitrogen`, then whatever that result is will be compared against `5`. If you want to compare both against `5`, you need to spell that out fully by writing out `<=` twice, or use `any`. – Carcigenicate Sep 23 '21 at 21:37

2 Answers2

0

and and or are operators, just like arithmetic and comparison operators. That means they have one operand on either side and resolve to a single value, which may be an operand for another operator.

Complex, English-like expressions are difficult to implement and can result in strange interactions that are hard for programmers to read, even if they know all the grammar rules. That's why you shouldn't assume that Python expressions work like English, unless you really know what you're doing.

Here is a possibly related question. It deals specifically with logical operators: How to test multiple variables against a single value?

luther
  • 5,195
  • 1
  • 14
  • 24
  • I don't see what your argument is with that first paragraph. – no comment Sep 23 '21 at 21:41
  • @don'ttalkjustcode: The question assumes that logical operators are special in some way. My first paragraph explains how they're not special. – luther Sep 23 '21 at 21:53
  • It's just that... you make it sound like *"`(a or b) <= c` isn't equivalent to `(a <= c) or (b <= c)`, just like it isn't for any other operators"*. But for example `(a or b) and c` with three bools *is* equivalent to `(a and c) or (b and c)`. And "programming doesn't work like English" doesn't explain how programming *does* work here. – no comment Sep 24 '21 at 10:08
  • `(a or b) and c` is a contrived example which doesn't illustrate how operators work and doesn't reflect the OP's code. 'And "programming doesn't work like English" doesn't explain how programming does work here.' Hence the first paragraph. The first paragraph explains what's really going on, and the second answers the question more directly by addressing the assumption that the OP seems to be making. – luther Sep 24 '21 at 14:05
  • Well, none of it explains what `or` does, so it seems like only half of the story. What's contrived about my example? It's just the closest to the original that I thought of. There are plenty of other examples of distributive operation pairs, e.g., `(a + b) * c` for ints. – no comment Sep 24 '21 at 14:12
  • It does explain that `or` is an operator and how operators work. That's all I see the question asking about. They're not asking about the details of `or` in particular. It's a more general principal that they're missing. Your example is contrived because just because *some* operators are distributive doesn't mean *all* of them are. Why bring that up when it's irrelevant to the question? – luther Sep 24 '21 at 14:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/237461/discussion-between-luther-and-dont-talk-just-code). – luther Sep 24 '21 at 14:51
0

I assume you have three numeric variables: mass, sodium, and nitrogen, and you want to compare them against some border values. Then, your condition should look like "if ((mass >= 2.6) and ((sodium <= 5) or (nitrogen <= 5))):" Actually, I think you can omit everything except the brackets after AND and below colon. However, I personally prefer to leave them explicitly so you have a clear grouping (whuch is probably not recommended by Python guidelines, but works).

If I got it right, your error is that you are trying to use OR between your numeric variables and then compare the result against 5, while you should be comparing each of the variables against 5 and then use OR to process Boolean results you got on step 1.

Alex
  • 815
  • 9
  • 19