[Tut] Python IndentationError: unexpected indent (How to Fix This Stupid Bug) - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] Python IndentationError: unexpected indent (How to Fix This Stupid Bug) (/thread-97675.html) |
[Tut] Python IndentationError: unexpected indent (How to Fix This Stupid Bug) - xSicKxBot - 10-10-2020 Python IndentationError: unexpected indent (How to Fix This Stupid Bug) <div><p>If you’re like me, you try things first in your code and fix the bugs as they come. One frequent bug in Python is the <code>IndentationError: unexpected indent</code>. So, what does this error message mean?</p> <p class="has-text-align-left has-pale-cyan-blue-background-color has-background">The error <em><code>IndentationError: unexpected indent</code></em> arises if you use inconsistent indentation of tabs or whitespaces for indented code blocks such as the <code>if</code> block and the <code>for</code> loop. For example, Python will throw an indentation error, if you use a <code>for</code> loop with <strong>three whitespace characters</strong> indentation for the first line, and <strong>one tab character</strong> indentation of the second line of the loop body. To fix the error, use the same number of empty whitespaces for all indented code blocks.</p> <figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"> <div class="wp-block-embed__wrapper"> <div class="ast-oembed-container"><iframe title="Python IndentationError: unexpected indent (How to Fix This Stupid Bug)" width="1400" height="788" src="https://www.youtube.com/embed/ceDih3Mb5yE?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div> </div> </figure> <div class="wp-block-image"> <figure class="aligncenter size-large is-resized"><img loading="lazy" src="https://blog.finxter.com/wp-content/uploads/2020/10/image-111.png" alt="Python Indentation Error Message Screenshot" class="wp-image-14793" width="572" height="182" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/image-111.png 763w, https://blog.finxter.com/wp-content/uploads/2020/10/image-111-300x95.png 300w, https://blog.finxter.com/wp-content/uploads/2020/10/image-111-150x48.png 150w" sizes="(max-width: 572px) 100vw, 572px" /></figure> </div> <p>Let’s have a look at an example where this error arises:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for i in range(10): print(i) print('--')</pre> <p>The first line in the loop body uses two whitespaces as indentation level. The second line in the <a href="https://blog.finxter.com/python-loops/" target="_blank" rel="noreferrer noopener" title="Python Loops">loop </a>body uses three whitespace characters as indentation level. Thus, the indentation blocks are different for different lines of the same block. However, Python expects that all indented lines have structurally the same indentation. </p> <h2>How to Fix Python’s Indentation Error?</h2> <p>To fix the error, simply use the same number of whitespaces for each line of code:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for i in range(10): print(i) print('--')</pre> <p>The general recommendation is to use four single whitespace characters <code>' '</code> for each <a href="https://blog.finxter.com/pep-8-hanging-indentation-and-closing-brackets-in-python/" target="_blank" rel="noreferrer noopener" title="PEP 8: Hanging Indentation and Closing Brackets in Python">indentation </a>level. If you have nested indentation levels, this means that the second indentation level has 4+4=8 single whitespace characters:</p> <pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">for i in range(10): for j in range(10): print(i, j)</pre> <h3>Mixing Tabs and Whitespace Characters Often Causes The Error</h3> <p>A common problem is also that the indentation seems to be consistent—while it really isn’t. The following code has one tab character in the first line and four empty whitespaces in the second line of the indented code block. They look the same but Python still throws the indentation error.</p> <div class="wp-block-image"> <figure class="aligncenter size-large is-resized"><img loading="lazy" src="https://blog.finxter.com/wp-content/uploads/2020/10/indentationerror-1024x576.jpg" alt="Python Indentation Error How to Fix" class="wp-image-14788" width="768" height="432" srcset="https://blog.finxter.com/wp-content/uploads/2020/10/indentationerror-scaled.jpg 1024w, https://blog.finxter.com/wp-content/uploads/2020/10/indentationerror-300x169.jpg 300w, https://blog.finxter.com/wp-content/uploads/2020/10/indentationerror-768x432.jpg 768w, https://blog.finxter.com/wp-content/uploads/2020/10/indentationerror-150x84.jpg 150w" sizes="(max-width: 768px) 100vw, 768px" /></figure> </div> <p>On first sight the indentation looks the same. However, if you go over the whitespaces before <code>print(i)</code>, you see that it consists only of a single tabular character while the whitespaces before the <code>print(j)</code> statement consists of a number of empty spaces <code>' '</code>. </p> <p><strong>Try It Yourself:</strong> Before I tell you what to do about it, try to fix the code yourself in our interactive Python shell:</p> <p> <iframe height="400px" width="100%" src="https://repl.it/@finxter/indentationError?lite=true" scrolling="no" frameborder="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals"></iframe> </p> <p><em><strong>Exercise</strong>: Fix the code in the interactive code shell to get rid of the error message.</em></p> <p class="wp-block-block">Do you want to develop the skills of a <strong>well-rounded Python professional</strong>—while getting paid in the process? Become a Python freelancer and order your book <a href="https://amzn.to/2Re2JqO" target="_blank" rel="noreferrer noopener" title="https://amzn.to/2Re2JqO"><strong>Leaving the Rat Race with Python</strong></a> on Amazon (<em>Kindle/Print</em>)!</p> <figure class="wp-block-image size-large is-resized"><img loading="lazy" src="https://blog.finxter.com/wp-content/uploads/2020/08/final_cover-683x1024.jpg" alt="Leaving the Rat Race with Python Book" class="wp-image-11850" width="205" height="308" srcset="https://blog.finxter.com/wp-content/uploads/2020/08/final_cover-scaled.jpg 683w, https://blog.finxter.com/wp-content/uploads/2020/08/final_cover-200x300.jpg 200w, https://blog.finxter.com/wp-content/uploads/2020/08/final_cover-768x1152.jpg 768w, https://blog.finxter.com/wp-content/uploads/2020/08/final_cover-1024x1536.jpg 1024w, https://blog.finxter.com/wp-content/uploads/2020/08/final_cover-1365x2048.jpg 1365w, https://blog.finxter.com/wp-content/uploads/2020/08/final_cover-150x225.jpg 150w" sizes="(max-width: 205px) 100vw, 205px" /></figure> <h2>How to Fix The Indentation Error for All Times?</h2> <p>The source of the error is often the misuse of tabs and whitespace characters. In many code editors, you can set the tab character to a fixed number of whitespace characters. This way, you essentially never use the tabular character itself. For example, if you have the sublime text editor, the following quick tutorial will ensure that you never run in this error ever again:</p> <ul> <li>Set <code>Sublime Text</code> to use tabs for indentation: <code>View</code> –> <code>Indentation</code> –> <code>Convert Indentation to Tabs</code></li> <li>Uncheck option <code>Indent Using Spaces</code> in the same sub-menu above.</li> </ul> <h2 class="wp-block-block">Where to Go From Here?</h2> <p>Enough theory, let’s get some practice!</p> <p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p> <p><strong>Practice projects is how you sharpen your saw in coding!</strong></p> <p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p> <p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p> <p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p> <p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p> </p> <p>The post <a href="https://blog.finxter.com/python-indentationerror-unexpected-indent/" target="_blank" rel="noopener noreferrer">Python IndentationError: unexpected indent (How to Fix This Stupid Bug)</a> first appeared on <a href="https://blog.finxter.com/" target="_blank" rel="noopener noreferrer">Finxter</a>.</p> </div> https://www.sickgaming.net/blog/2020/10/10/python-indentationerror-unexpected-indent-how-to-fix-this-stupid-bug/ |