Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
2.60/5 (3 votes)
See more:
give me examples of sample programs
Posted

1 solution

You question is a bit vague, but I'll try to explain what I thought you were asking. As I understand your question you want to calculate the indentation depth to which I have two interpretations each with different answer as one might expect.

Q 1: The line is already properly indented and you want the indentation depth.

A 1: Take all the white space from the beginning of the line and if it is all tabs just return the length of this all white space string.
If it is spaces you you'd have to have knowledge of the least number of indenting spaces found in this file so far greater than zero and then make sure the current white space count can be divided without remainder by that. The result of said division is the indentation level
If you encounter mixed indentation characters something has run afoul and you should return some error stating this.

Q 2: Given some code without indentation how can I calculate the indentation depth of each line?

A 2: You can't! Indentation in Python has block semantic like ENDIF in VB.NET or braces {} in C#. You can change the indentation of a Python program still leaving you with valid code, but the semantics may have changed completely.

Regards,
— Manfred
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900