Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Python uses spacing at the start of the line to determine when code blocks start and end. Errors you can get are:</p> <p><strong>Unexpected indent.</strong> This line of code has more spaces at the start than the one before, but the one before is not the start of a subblock (e.g. if/while/for statement). All lines of code in a block must start with exactly the same string of whitespace. For instance:</p> <pre><code>&gt;&gt;&gt; def a(): ... print "foo" ... print "bar" IndentationError: unexpected indent </code></pre> <p>This one is especially common when running python interactively: make sure you don't put any extra spaces before your commands. (Very annoying when copy-and-pasting example code!)</p> <pre><code>&gt;&gt;&gt; print "hello" IndentationError: unexpected indent </code></pre> <p><strong>Unindent does not match any outer indentation level.</strong> This line of code has fewer spaces at the start than the one before, but equally it does not match any other block it could be part of. Python cannot decide where it goes. For instance, in the following, is the final print supposed to be part of the if clause, or not?</p> <pre><code>&gt;&gt;&gt; if user == "Joey": ... print "Super secret powers enabled!" ... print "Revealing super secrets" IndendationError: unindent does not match any outer indentation level </code></pre> <p><strong>Expected an indented block.</strong> This line of code has the same number of spaces at the start as the one before, but the last line was expected to start a block (e.g. if/while/for statement, function definition).</p> <pre><code>&gt;&gt;&gt; def foo(): ... print "Bar" IndentationError: expected an indented block </code></pre> <p>If you want a function that doesn't do anything, use the "no-op" command <em>pass</em>:</p> <pre><code>&gt;&gt;&gt; def foo(): ... pass </code></pre> <p>Mixing tabs and spaces is allowed (at least on my version of Python), but Python assumes tabs are 8 characters long, which may not match your editor. <strong>Just say "no" to tabs.</strong> Most editors allow them to be automatically replaced by spaces.</p> <p>The best way to avoid these issues is to always use a consistent number of spaces when you indent a subblock, and ideally use a good IDE that solves the problem for you. This will also make your code more readable.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload