-1

I am new to python, I am doing the exercise here(http://www.learnpython.org/page/Loops). For the following code:

   numbers = [
        951, 402, 984, 651, 360, 69, 408, 319, 601, 485, 980, 507, 725, 547, 544, 
        615, 83, 165, 141, 501, 263, 617, 865, 575, 219, 390, 984, 592, 236, 105, 942, 941, 
        386, 462, 47, 418, 907, 344, 236, 375, 823, 566, 597, 978, 328, 615, 953, 345, 
        399, 162, 758, 219, 918, 237, 412, 566, 826, 248, 866, 950, 626, 949, 687, 217, 
        815, 67, 104, 58, 512, 24, 892, 894, 767, 553, 81, 379, 843, 831, 445, 742, 717, 
        958, 609, 842, 451, 688, 753, 854, 685, 93, 857, 440, 380, 126, 721, 328, 753, 470, 
        743, 527
    ]

    # your code goes here
    for number in numbers:
        if number <= 237 and number%2 == 0:
            print number

I got the following problem:

Traceback (most recent call last):
  File "/base/data/home/apps/s~learnpythonjail/2.365841894475711898/main.py", line 75, in execute_python
    exec(code, {})
  File "<string>", line 14
    print number
        ^
IndentationError: expected an indented block

May I know how to resolve it?

3
  • 4
    Are you mixing tabs and spaces? Commented Mar 27, 2013 at 16:13
  • @MartijnPieters, looking at the post source, it seems like it. the if line has 8 spaces and a tab. The print line has 12 spaces and a tab. Commented Mar 27, 2013 at 16:15
  • 1
    Configure your editor to only use spaces for indentation. It is too easy to mix tabs and spaces and create inconsistent indentation otherwise. Commented Mar 27, 2013 at 16:17

3 Answers 3

5

The error means that print is not indented beyond the level of the preceding if line.

If, in your editor, it looks like the print line is indented properly anyway, you are mixing tabs and spaces inconsistently. Replace all tabs with spaces to correct.

You can configure most editors to use spaces only for indentation; this is what the Python styleguide (PEP 8) recommends:

Tabs or Spaces?

Never mix tabs and spaces.

The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only. Code indented with a mixture of tabs and spaces should be converted to using spaces exclusively. When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!

For new projects, spaces-only are strongly recommended over tabs. Most editors have features that make this easy to do.

Sign up to request clarification or add additional context in comments.

Comments

2

Here is what your code looks like in an editor that displays all of the whitespace characters with tabs (tab size set to 4):

enter image description here

The small dots are space characters, and the arrows are tabs. As noted in the other answers, mixing tabs and spaces is a bad idea. The above code may look okay but look at what happens if you change the tab size to 8 characters:

enter image description here

This is the root cause of the error you are seeing. When you mix tabs and spaces the indentation that you see in your editor may not be the same indentation that the Python interpreter sees.

Comments

1

I've reproduce your case: one table before the if statement, and 8 spaces before the print. Please use spaces only instead.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.