dimanche 3 juin 2018

The if statement executing on False

I tried one python program and noticed something completely unusual, I saw if statement getting executed on False. I neither have any idea how it happened nor found any relevant answer. Please check the if block commented above as 'faulty if' and tell me why it is getting executed on 'False'.

import sys
def click(arr, r, c, x, y):
if abs(arr[x][y]) != 1:
    l, extra = [], []
    l.append([x, y])
    while l != []:
        x, y = l.pop()
        extra.append([x, y])
        arr[x][y] = -2
        no_check = ''
        if x == 0:
            no_check += 'r'
        elif x == r - 1:
            no_check += 'R'
        if y == 0:
            no_check += 'c'
        elif y == c - 1:
            no_check += 'C'

        if 'r' not in no_check and arr[x - 1][y] == 0\
        and [x - 1, y] not in l and [x - 1, y] not in extra:
            l.append([x - 1, y])


        #This 'if' is faulty
        if (('R' not in no_check and arr[x + 1][j] == 0)\
        and (([x + 1, y] not in l) and ([x + 1, y] not in extra))):
            print('Entered')
            l.append([x + 1, y])
            print("But condition is", (('R' not in no_check and arr[x + 1][j] == 0)\
        and (([x + 1, y] not in l) and ([x + 1, y] not in extra))))
        sys.exit()
return arr

field = [[0, 0, 0, 0, 0],
         [0, 1, 1, 1, 0],
         [0, 1, -1, 1, 0]]
rows, cols, = 3, 5
i, j = 0, 0
new_field = click(field, rows, cols, i, j)
print()
for a in new_field:
    for ele in a:
        print('%2d' % ele, end = ' ')
    print()

Aucun commentaire:

Enregistrer un commentaire