samedi 16 mai 2020

If, else return else value even when the condition is true, inside a for loop

Here is the part of the code which is buggy:

n = len(data)
for i in range(n):
    count = 0
    if data[i:i + l] == field:
        while data[i - l: i] == data[i:i + l]:
            count = count + 1
            i = i + 1
    else:
        print("OK")

It works in some cases and given incorrect output in most cases. I checked by printing the strings being compared, and even after matching the requirement, the loop results in "OK" which is to be printed when the condition is not true! I don't get it!

Aucun commentaire:

Enregistrer un commentaire