[Tutor] Using break, else with for loop
Manprit Singh
manpritsinghece at gmail.com
Sun Oct 11 02:38:09 EDT 2020
Dear sir ,
Consider a problem where I have to check if a list contains an odd number.
If the list contains an odd number then the program should be capable of
displaying "Odd number found in list" and if there is no odd number present
in the list, the program must display "List does not contain odd number".
Just need to check if my approach to solve this problem is right or not .
The solution done by me is given below :
l = [2, 4, 6, 5, 8, 3]
for i in l:
if i % 2 != 0:
print("Odd number found in list")
break
else:
print("List does not contain odd number")
Regards
Manprit Singh
More information about the Tutor
mailing list