Hi, My friends,

I am new to python. I try to figure out how python knows the end of loop or the end of a block of codes in while... else code
Here is the example:
while E:
Bt
else:
Bf


here if Bf have 20 lines of code. How the python knows which line of code is the end of else:

It looks like I miss something, which is quite different from all other languages.

Thanks in advance.

John

Recommended Answers

All 5 Replies

Hi,
In python, blocks structure rely on indentation

while E:
    Bt
else:
    Bf
back to the while level

Hi,
In python, blocks structure rely on indentation

while E:
    Bt
else:
    Bf
back to the while level

Thanks a lot. But this is kind of risky for a large project as you may have many loop and if to have indentations.

John

Well, maybe... I don't think this may be a problem. it's just like when you have to be careful about {{{ and }}}.
The positive point of that is that you HAVE to be careful of your code readability.
Python is compact and clear (and that's why I use it instead of perl for instance)

Thanks a lot. But this is kind of risky for a large project as you may have many loop and if to have indentations.

John

Not really. For your large project you factor and re-factor the code, to improve maintainability which also improves readability.

People have add-hoc rules like "a function should fit on a screen", or "excessive textual nesting of loops and conditionals is bad"; they are good for any language. It usually means looking at a large function and finding out why it is large with a view to encapsulating some of its functionality in another function that will be called instead.

- Paddy.

I understand that most editors written for Python will make you aware of indentation problems, since they are so important for this language.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.