PYTHON: I got an "if" statement nested in a "for" loop. I need to return a defalut value if, at the end of the for loop, no one of the if statement was satisfied.
example: print the missing numbers in a given list or return the number next to the last element in list
list1 = [0, 1, 2, 3, 5, 7]
FOR i in range(list1[0], list1[-1]):
IF (i not in list1):
print(i) {#prints 4, 6}
ELSE (if 4, 6 were in list1 and so there were not missing numbers in the range):
print(list1[-1] + 1) {#prints the number right after the last item of list}
i knew it's possible to do an else statement regarding a while loop, and i was wondering if the for loop has the same possibility
Aucun commentaire:
Enregistrer un commentaire