How do I rectify the error "unexpected indent" in python?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
All You need to do is remove spaces or tab spaces from the start of following codes
If you're writing Python using Sublime and getting indentation errors,
view -> indentation -> convert indentation to spaces
The issue I'm describing is caused by the Sublime text editor. The same issue could be caused by other editors as well. Essentially, the issue has to do with Python wanting to treat indentations in terms of spaces versus various editors coding the indentations in terms of tabs.
Simply copy your script and put under """ your entire code """ ...
specify this line in a variable.. like,
now execute this code, in Sublime Editor using ctrl+b, now it will print indented code in console. that's it
Turn on visible whitespace in whatever editor you are using and turn on replace tabs with spaces.
While you can use tabs with Python mixing tabs and space usually leads to the error you are experiencing. Replacing tabs with 4 spaces is the recommended approach for writing Python code.
There is a trick that always worked for me:
If you got and unexpected indent and you see that all the code is perfectly indented, try opening it with another editor and you will see what line of code is not indented.
It happened to me when used vim, gedit or editors like that.
Try to use only 1 editor for your code.
By using correct indentation. Python is whitespace aware, so you need to follow its indentation guidlines for blocks or you'll get indentation errors.