vendredi 23 août 2019

If else with an and condition not executing underlying statement

I am trying to run an if -else conditions in python but even after satisfying all the condition in if-else it is not executing the underlying statements . What can be the issue here ..

def solution(N):
    binary_form = bin(N)[2:]
    print(binary_form)
    biggest_gap = 0
    start_count = 0
    present_gap = 0

    for digits in binary_form:
        print("digits ",digits ,"  and start_count is ",start_count)
        if digits == 1 and start_count == 1:
            print("start count vlaue is ", start_count)
            start_count = 0
            present_gap = 0
        elif digits == 1 and start_count == 0:
            print("start count vlaue is ", start_count)
            start_count = 1
            present_gap = 0
        elif digits == 0 and start_count == 1:
            print("start count vlaue is ", start_count)
            present_gap = present_gap + 1
            print("present gap  ", present_gap)
        if present_gap > biggest_gap:
            biggest_gap = present_gap

    # print(binary_form)
    return biggest_gap


def main():
    N = 5
    binary_gap = solution(N)
    print(binary_gap)


if __name__ == "__main__":
    main()

I get below result on running this code .

101
digits  1   and start_count is  0 
digits  0   and start_count is  0 
digits  1   and start_count is  0
0

The expected output should be

101
digits  1   and start_count is  0 
start count vlaue is 0
digits  0   and start_count is  0 
digits  1   and start_count is  0
start count vlaue is 0
1

Aucun commentaire:

Enregistrer un commentaire