Click here to Skip to main content
11,795,848 members (82,515 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: Python
give me examples of sample programs
Posted 3-Jun-13 2:41am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

— Manfred

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

  Print Answers RSS
0 Maciej Los 410
1 Abhinav S 368
2 OriginalGriff 300
3 Sergey Alexandrovich Kryukov 266
4 KrunalRohit 249
0 OriginalGriff 1,852
1 Maciej Los 1,465
2 KrunalRohit 1,300
3 CPallini 840
4 Richard MacCutchan 828

Advertise | Privacy | Mobile
Web02 | 2.8.151002.1 | Last Updated 3 Jun 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100