jeudi 4 février 2021

How to check for add or subtract in python [duplicate]

first of all please don't delete my question. I have looked all over the place and couldn't find an answer to my question, mainly because i don't really know how to ask it. Lets say I have 2 variables, a and b, in a for loop:

for a, b:
    if a + b == c:
        break

problem is i want to check for subtraction AND addition. Since here there are only 2 variables, I can write all the combinations

for a,b:
    if (a + b == c) or (a - b == c) or (b - a == c):
        break

problem is i have much more than 2 variables. So let's say about 6 (I'm not sure because it varies in my program, I get the values from a 2D arrays with rows of varying length) Is there anyway I can do something like this:

for a,b,c,d,e,f,g
    if (+- a +- b +- c +- d +- +- e +- f +- g == h):
       break

I hope this makes sense. Sorry my English isn't that great, and I just don't really know how to look this question up on google or StackOverflow because I don't know what the hell this is called, so please don't delete my question. Also don't really know how to ask it, so i just hoped it would make sense if I showed an example like this.

Aucun commentaire:

Enregistrer un commentaire