Here is the problem. There are two types of else
statements in this case. If you align the else
statement with while
, the else
statement is executed when the condition of the while
loop becomes false.
In your code, the else statement gets executed when position < len(numbers)
is not true.
Also, the syntax problem is occurring just because you have a line of code between the if
and else
statements, which is position += 1
If you want to use an else
statement for your if
statement (not for the while
statement as I suggested at the beginning), you should move this line of code in between of if
and else
.
Try this:
while position < len(numbers):
number = numbers[position]
if number % 2 == 0:
print('Found even number', number)
break
else:
print('No even number found')
position += 1
Hope this helps.