1

The code below works such that an array is an input in a function. This 2 element array is iterated such that it would stop if the difference between the new and previously iterated array values equals zero (or it is intended to work as such). Note that the function below is just a pseudo function.

Is using a "OR" and "AND" operator appropriate for what I want. If so, which is best to use and if not, what is a better method?

def func(array):
   
   counter = 0
   diff = True 
   array_i = array
   while diff: 
       array_f = array_i + 1/array_i
       diff = abs(array_i[0] - array_f[0])  or abs(array_i[1] - array_f[1]) > 0 
       array i = array_f 
       counter += 1
   return array_i, counter 
SyntaxError101
  • 79
  • 1
  • 2
  • 11
  • 2
    `x or y > 0` does not work the way you think it does – Aaron Apr 30 '21 at 02:46
  • @Aaron as I suspected. Please elaborate why? – SyntaxError101 Apr 30 '21 at 02:47
  • I'm sure someone will search one of the million duplicates, but basically it boils down to operator order (think [pedmas](https://en.wikipedia.org/wiki/Order_of_operations)). `x or y` is calculated before the `>`, so it would look like this with paranthesis: `(x or y) > 0` – Aaron Apr 30 '21 at 02:49
  • @Aaron so is it best to do ``(x < 0 ) or (y < 0)`` ? – SyntaxError101 Apr 30 '21 at 02:51
  • 1
    every language implements an order of operations, and most of them are very similar, so this type of thing is fairly universal. Comparison operators similar to assignment operators have very low precedence (gets computed last), to make the typical case of comparing the result of a calculation generally require fewer parenthesis. – Aaron Apr 30 '21 at 02:51
  • 1
    yes that would work, I'm not aware of a good explanation of operator precedence from the official documentation, but [here's](https://www.mathcs.emory.edu/~valerie/courses/fall10/155/resources/op_precedence.html) a pretty straightforward layout. Edit... nvm it's just at the end of a super long-winded page on the [official docs](https://docs.python.org/3/reference/expressions.html#operator-precedence) – Aaron Apr 30 '21 at 02:53
  • **EDIT!** actually `or` is calculated after `>` my bad... it still is a bit messed up from the design intent, but it rather becomes `x or ( y > 0)` instead of `(x or y) > 0` – Aaron Apr 30 '21 at 03:03
  • 3
    The issue isn't precedence. It's that logical connectives don't automatically distribute like that like they do in English. "The ball is green or red" is equivalent to "the ball is green or the ball is red", and human listeners know this. That isn't how programming languages tend to work. – juanpa.arrivillaga Apr 30 '21 at 03:03
  • @GitGoodCodes it's not a matter of what's *best*, `x < 0 or y < 0` is *totally different* than `x or y < 0`. – juanpa.arrivillaga Apr 30 '21 at 03:20

1 Answers1

0

The logical operator or is used when you want to check a condition or another condition. The and operator is when both are to be combined.

Checks to see if either one or the other are greater than zero.

abs(array_i[0] - array_f[0]) > 0 or abs(array_i[1] - array_f[1]) > 0 

checks to see if both are greater than zero.

abs(array_i[0] - array_f[0]) > 0 and abs(array_i[1] - array_f[1]) > 0 
Daniel
  • 61
  • 1
  • 6