While I was practising in Python, I used two different editors, VSCode and Notepad, as I had to write my program on different computers as I went out for a few weeks.
But, in Notepad, unlike VSCode, the editor didn’t make necessary indentation, so I manually indented them with spaces.
But when running the program, I encountered this error,
TabError: inconsistent use of tabs and spaces in indentation
I simply don’t know what this is, and would like someone to explain.
I saw a question on this, "inconsistent use of tabs and spaces in indentation", but in Notepad, there was no option which was mentioned in the answers.
CodePudding user response:
TabError: inconsistent use of tabs and spaces
is raised when you mix up tabs and spaces for your indentation in Python.
When you use both tabs and spaces for indentation, Python interpreter raises an Exception, which is TabError.
So don’t mix up tabs and spaces while indenting Python code.
Either just use spaces, or use just spaces.
But I recommend to use tabs, as they are much easier to use.
By the way, I don’t think there is an option to do that in Notepad. Try any other IDE, or an online editor.
CodePudding user response:
Python depends on having correct indentation to recognize lines within the same codeblocks. This is what keeps statements grouped together and organized in a way that Python can understand them in the correct order. While 4 spaces or 1 tab are typically seen as the commonly followed indentation practice in Python, you could realistically do whatever you wanted as long as your indentation lined up in blocks where components need to mesh. For example, using an if statement with 3 spaces of indentation will not be considered in the same codeblock as a followup else statement that has 5 spaces of indentation.
The issue you're experiencing with Notepad is a result of basic Notepad not being a very good IDE. Stick with VSCode.
CodePudding user response:
You have mixed spaces and tabs. to solve this problem, you can replace all tabs with spaces in vscode using the command: ctrl H. Also, you can always use the online editor online_vscode
CodePudding user response:
This is because tab and whitespace are mixed in your code.
for i in loop:
print(i) # with tab
print(i) # with 4 spaces
>>> TabError: inconsistent use of tabs and spaces in indentation
This code will make exception, such as your TabError: inconsistent use of tabs and spaces in indentation due to mixed use of tab and whitespace indentations.
You have to choose one between tab and 4 spaces (it can be any number of spaces, actually).
To solve your problem in Notepad, you can replace all the 4 spaces to tab, as follows: https://www.tenforums.com/tutorials/114308-find-replace-text-notepad-windows-10-a.html
