0
Is it possible to add an else statement with a loop
is it possible
3 Respuestas
+ 2
yes it is possible.
for "while" and "for" loops, the else condition is executed in case that the loop was left under normal conditions, this means no "break" occured within the loop
Further, even a "try" statement can have an "else". In this case the else condition is executed when no error occured within the try-block
+ 5
I believe this is a case of trying to verify something using a for loop, and, if all the iterations are false, then print something. You can do it using booleans, e.g
arr = [5, 3, 9, 7, 2]
isAllOdd = True
for i in arr:
if i%2 == 0:
isAllOdd = False
if isAllOdd:
print('All members are odd.')
else:
print('arr contains even values.')
+ 1
for i in range 10
if i%2 == 0:
print(i)
else:
print("ODD")
#we have a for loop
#containing an if / else statement
#this code print every even number
#and replace odd number by "ODD"