This error mainly occurs when a code is indented using both space and tab.
It is always preferred either to use tab or space and not both at the same time. Tab and space are two different things in Python. There is no specific rule for using tab. Python just expects the same number of space used in first line to be same for rest of the code.
So when a code is indented with space for the first line, tab with the second line, it throws a TabError.
m=int(input(“ enter a integer M:”))
n=int(input(“ enter a interger N:”))
Print (“M is greater than N")
Print(“M and N are equal")
Print(“N is greater than M")
This code is one such example.
Ways to Solve Error
- To fix this error, it is always preferred to make sure that the code has proper indentation. Another way to solve this is, select the entire code by pressing CTRL+A key, then in idle go to format setting and select UNTABIFY REGION. This method is for IDLE.
- Convert all the indentation either to tab or to space. Show all commands tab by pressing CTRL+SHIFT+P, select ‘convert indentation’. Convert all the indentation for space or tab. This method is for VS code.
- Add this in the beginning of the code #!/usr/bin/python -tt
- Select all code block, press Shift and tab to align code block to the left then hit the tab bar number of time you want to indent the code.
These are the several methods used for avoiding taberror.
Always check for the wrong white space or tab in your code. Check for the particular number of space to be used for every line.