Python indentation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Python indentation

I am having python 2.7... what I am doing is that I wrote set of statements in notepad++ file having extension .py... for indentation, I m using four spaces.. when I copy statements from .py file and paste it into python exe, it gives me indentation error.. any idea would be of great help.

27th Apr 2019, 7:30 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
5 Answers
+ 4
If you can show your code then that would be great. As most of those who describe their problems, it sounds like they did nothing wrong. By "paste into python exe", do you mean running the code directly in the interpreter?
27th Apr 2019, 7:49 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Ketan, when you paste your code into your IDE, what product do you use? Have you tested it in SoloLearn playground, or do you use a windows, linux or ios environment?
27th Apr 2019, 9:02 AM
Lothar
Lothar - avatar
+ 3
it worked now... yes i was trying to put into python interpreter... environment is windows 10... what I did was copy one line and paste it into interpreter... don't copy spaces and provide one or two tab required as per indentation... completed all lines on interpreter...copy all lines from interpreter and pasted it into .py file.. thanks guys for responding on this
27th Apr 2019, 12:36 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 3
Mixing up tabs and spaces in python code for indentation is not a good idea. If you create your code all in the same environment / IDE you should not run in problems. Python recommends to use 4 spaces for indent, but if you have to remove indent you have to press Del or Backspace 4 times. This is the reason why many coders use tabs. IDE allows to define what 1 tab means in number of spaces. Some can handle so called soft tabs. This means if you use in a case number of spaces (as defined), you can delete them just with one click. IDE can also check ( and this is a very helpful feature) if you paste some code from whatever source into the code esitor against spaces and tabs. If the data you want to insert does not match the rules of your editor, it will ask to convert tabs / spaces before performing the action.
28th Apr 2019, 9:52 AM
Lothar
Lothar - avatar
+ 2
it sounded very strange, maybe it happened because there was a lost TAB instead of four spaces in your code, I would suggest using the find replace tool to substitute any TAB into spaces. Since you already solve this, this might help others
28th Apr 2019, 3:58 AM
Gabriel Quintela
Gabriel Quintela - avatar