dimanche 7 novembre 2021

Element out of bounds printout as "None"

The code below is an intent of printing out a list within the bounds of it. The list currently shows "None" after the last element (Yes, I know "None" in python is "Null" in C++), but I can't see why the while loop goes beyond the boundaries of the list when the "if" loop breaks it if it finds the element to be a "None"

def out_of_boud(lst):
    lst_length = int(len(lst))
    element = int(0)

    while (element >= 0 and element < lst_length):
        if element == None: break
        else:
            print(str(lst[element]) + "\t" + str(element))
            element +=1

print(out_of_boud([10, 15, 20, 21, 22, 35]))
print(out_of_boud([10, 15, 20, 21]))

Aucun commentaire:

Enregistrer un commentaire