Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Python
give me examples of sample programs
Posted 3-Jun-13 3:41am

1 solution

Rate this: bad
good
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.
 
Regards,
— Manfred
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 3 Jun 2013
Copyright © CodeProject, 1999-2014
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