How to check, out of multiple if statements, at least one if statement is run?
Question:
I know the question doesn’t really make sense to you. Let me show you an example below.
if_group:
if i == 0:
answer[i][j] += check(matrix, i, j, "down")
if j == 0:
answer[i][j] += check(matrix, i, j, "right")
if i == len(matrix) - 1:
answer[i][j] += check(matrix, i, j, "up")
if j == len(matrix[i]) - 1:
answer[i][j] += check(matrix, i, j, "left")
else:
do something
Let’s say, if at least one of those 4 if statements inside the if_group is run, then skip the else statement. However, if none of those if statements is run, then execute the else statement.
Answers:
You can add a check variable, that will be checked at the end of ifs and run the else condition only when no if was met. See below:
check=0
if i == 0:
answer[i][j] += check(matrix, i, j, "down")
check=1
if j == 0:
answer[i][j] += check(matrix, i, j, "right")
check=1
if i == len(matrix) - 1:
answer[i][j] += check(matrix, i, j, "up")
check=1
if j == len(matrix[i]) - 1:
answer[i][j] += check(matrix, i, j, "left")
check=1
if check==0:
do something
You can keep the results of your conditions in an array of bools, and then ask about the array’s sum. This is taking advantage of "True" interpreted as "1". So sum(array_of_bools) is basically asking "how many of my conditions are True?". also, python 3.8 introduces assignment expressions, which allows doing something like this:
cond = [False * 4]
if cond[0] := i == 0:
answer[i][j] += check(matrix, i, j, "down")
if cond[1] := j == 0:
answer[i][j] += check(matrix, i, j, "right")
if cond[2] := i == len(matrix) - 1:
answer[i][j] += check(matrix, i, j, "up")
if cond[3] := j == len(matrix[i]) - 1:
answer[i][j] += check(matrix, i, j, "left")
if not sum(cond):
do something
I know the question doesn’t really make sense to you. Let me show you an example below.
if_group:
if i == 0:
answer[i][j] += check(matrix, i, j, "down")
if j == 0:
answer[i][j] += check(matrix, i, j, "right")
if i == len(matrix) - 1:
answer[i][j] += check(matrix, i, j, "up")
if j == len(matrix[i]) - 1:
answer[i][j] += check(matrix, i, j, "left")
else:
do something
Let’s say, if at least one of those 4 if statements inside the if_group is run, then skip the else statement. However, if none of those if statements is run, then execute the else statement.
You can add a check variable, that will be checked at the end of ifs and run the else condition only when no if was met. See below:
check=0
if i == 0:
answer[i][j] += check(matrix, i, j, "down")
check=1
if j == 0:
answer[i][j] += check(matrix, i, j, "right")
check=1
if i == len(matrix) - 1:
answer[i][j] += check(matrix, i, j, "up")
check=1
if j == len(matrix[i]) - 1:
answer[i][j] += check(matrix, i, j, "left")
check=1
if check==0:
do something
You can keep the results of your conditions in an array of bools, and then ask about the array’s sum. This is taking advantage of "True" interpreted as "1". So sum(array_of_bools) is basically asking "how many of my conditions are True?". also, python 3.8 introduces assignment expressions, which allows doing something like this:
cond = [False * 4]
if cond[0] := i == 0:
answer[i][j] += check(matrix, i, j, "down")
if cond[1] := j == 0:
answer[i][j] += check(matrix, i, j, "right")
if cond[2] := i == len(matrix) - 1:
answer[i][j] += check(matrix, i, j, "up")
if cond[3] := j == len(matrix[i]) - 1:
answer[i][j] += check(matrix, i, j, "left")
if not sum(cond):
do something