Indenting every action?


Why, for the code below, do I have to indent both print lines (and indent each print separately)?

pyg = ‘ay’

original = raw_input(‘Enter a word:’)

if len(original) > 0 and original.isalpha():
word = original.lower()
first = word[0]
print word
print first
print ‘empty’


yes, you have to indent both lines, given they are nested inside the if clause.

a print statement is capable to print multiple things, but that is up to you.



For this example I don’t see the sense. Creation of a variable is sometimes indented separately, sometimes can be the same as another variable.


indent has nothing to do with the creation of variable or print statement, it has to do with nesting. Nesting code inside an if clause or a function for example. Where many languages use curly brackets to determine nesting:

if (condition){
    print "i am inside the function"
print "indent doesn't matter"
print "i am outside the function"

python use indent to determines nesting:

if condition:
    print "i am nested inside the if clause"

print "i am nested outside the if clause"
    print "this line is invalid, wrong indent level"


when I didn’t indent this way, the editor threw indentation errors “IndentationError: unindent does not match any outer indentation level”


make sense, that is what i am trying to explain. How vital correct indent is within python


But there’s no clear logic why indent 1 variable, but not the other.
If I nest them ALL inside the if on the same level, they throw an error. Why is that?


i tried to explain nesting concept, all code that belongs in the if clause, should have the same indent leve

because the amount of indent needs to be same, looking at your code:

  word = original.lower()
    print word
  first = word[0]
    print first

these 4 lines do no have the same indent level, while they should. The amount of spaces at the beginning of the line is important, they need to be equal


If I indent them all the same (but all inside if, by 2 spaces), the editor throws an error :slight_smile:

For example - see attached image


so then all the indent within the if clause is the same? Please copy paste your code to the forum so i can confirm

i can’t run screenshot codes

the editor is sometimes a bit picky, you might want to re-indent around line 7


Codecademy’s editor is arguably misconfigured when it comes to inserting and displaying tab characters (of which there usually shouldn’t be any in python code)

And, yes, code isn’t pictures. The way you want to share it is as an exact copy of the text, or better yet as the file itself but text will usually accomplish this because we generally all use utf8 when encoding it.
(the goal here is that whoever you show something to, should be able to reproduce it)


Apologies for not pasting as code

pyg = 'ay'

original = raw_input('Enter a word:')

if len(original) > 0 and original.isalpha():
	word = original.lower()
  print word
	first = word[0]
  print first
  new_word = word[1:] + first + pyg
  print new_word
  print 'empty'


Right, but, I don’t need it any more - mystery solved (and this webpage displays it better than codecademy’s editor, as you see that’s not aligned, and if you highlight it with your cursor you’ll find that that isn’t spaces, it’s tabs)

This is certainly one reason why tabs are a bad idea. They are “smart” unlike almost every other character, leading to unexpected things and tools handling them poorly unless they are well configured and happen to care about tabs

Not talking about the tab key here by the way. Editors should generally remap that key to mean “indent” instead of a literal tab character


Thanks, that was it. I’m not sure how you saw it here, highlighting with cursor?

At any rate, great help, much appreciated. How can I remap “tab key” to mean “indent”? Inside a code editor, you mean?


If you click and drag so the text turns blue, and move your cursor one character at a time, it should grow by 1 character each time, but there’s a large skip over the indentation, since the character there is very wide

That’s not how I saw it though, I deduced it from that you saw it aligned visually, but that it wasn’t. Tabs fit that behaviour.

A code editor typically has settings for the tab key telling it how to react to that keypress.


I understand now, indeed tabs show as large spaces that cannot be divided, whereas multiple spaces appear as separate items.

Thanks again!


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