Https://www.codecademy.com/courses/learn-python/lessons/python-2-loops/exercises/your-own-while--else

Dear Fellow colleagues
https://www.codecademy.com/courses/learn-python/lessons/python-2-loops/exercises/your-own-while--else

Why does while and else have to be at the same indentation level and why is it wrong if the else statement is under the while loop?

Thank you jagmeet

The else has to be at the same indentation in order for the program to know which statement the else is referring to.

I don’t quite get it

The reason the else has to be on the same indentation level is so that when the program is running, it ‘knows’ that the else is connected to the while-computers aren’t smart, they’re just good at following instructions. Indentation is Python’s way of following instructions-it tells the computer which statements/blocks are related.

The same rules that apple for if/else apply for while/else.

1 Like

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.