3

I am a newbie in python and using the python shell through a linux system. I enter the python shell by typing "python" at the command line. When i try to execute a for-loop from the python shell, the shell does not allow me to further continue with indentation and sends File "", line 2 error. Please check the code below;

ash-4.1$ python
Python 2.6.6 (r266:84292, May 22 2015, 08:34:51)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-15)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> word=("cheese")
>>> word
'cheese'
>>> for character in word:
... print(character)
  File "<stdin>", line 2
    print(character)
        ^
IndentationError: expected an indented block
>>>

Also the version installed on this linux system is 2.6. Can you please help me how to work through this?

Chris_Rands
  • 38,994
  • 14
  • 83
  • 119

4 Answers4

2

You need to indent your loop body

word=("cheese") 
for character in word:
       print(character) 
Joey
  • 344,408
  • 85
  • 689
  • 683
Yousaf
  • 27,861
  • 6
  • 44
  • 69
  • I want to put further statements in the inner block of my loop. but the shell doesnt allow after the first statement of the inner block of code (... print(character))... the moment i press another enter here i receive that indentation error and thrown out of the code block and receive the shell prompt (>>>). I think there must be some end of line character i can replace??? Not sure... – Diganto M. Paul Jan 13 '17 at 01:38
0

Indent the second line of the loop by four spaces and then hit Enter. Maintain the same indentation throughout the loop body, and terminate the loop with a blank line.

EDIT: Your code will not work, however, because you're regarding word as a list of characters. Python doesn't work like that.

Anomitra
  • 1,111
  • 15
  • 31
  • Didn't work >>> words=["cheese","pizza","apples","lettuce"] >>> import random >>> word=words[random.randint(0,len(words)-1)] >>> for character in words: ... print(character) /// the moment i press any Enter it throws me the below error.// File "", line 2 print(character) ^ IndentationError: expected an indented block >>> – Diganto M. Paul Jan 13 '17 at 01:34
0

In Python indentation pretty much work like braces in other languages like C++ or Java.

In C++/Java a for-loop would look like:

for(int i = 0; i < 100; i++) {
    System.out.println(i);  // Java
    std::cout << i << std::endl; // C++
}

Be aware, that this is pseudo-code, as it is a mix of Java and C++ Code. The indentations are not needed, since the {} indicate the body of the for-loop. For better readability they are still used.

In Python, however, your only way to tell the intepreter something is within the body of a function or in this case loop would be by indenting the body, such as:

for i in range(100):
    print(i)

This applies also for functions:

C++/Java:

public int square(int x) {
    // body of function
    return x*x;
}

Python:

def square(x):
    return x*x;
macskay
  • 381
  • 1
  • 2
  • 18
  • Thanks.I have searched through the internet but i couldnt find my answer. If i write a python code in an editor (notepad/vi) and then execute it via python, it runs.. but if i am using the installed python shell in my linux system to write this small code where i need some indentation, I get thrown out of the code block to the shell prompt! >>> for character in words: ... print(character) File "", line 2 print(character) ^ IndentationError: expected an indented block The moment i press Enter after (... print(character)), i receive that error and the shell prompt. – Diganto M. Paul Jan 13 '17 at 01:42
  • That happens, when the editor you used use different identation styles, i.e. one using tabs, the other one using 4 spaces. When this is mixed up the Interpreter throws an error. To avoid this problem set all your editors to use the same style. I personally set all my IDEs and editors to use 4 spaces, when pressing TAB. – macskay Jan 14 '17 at 02:44
  • Noted... Thanks!! – Diganto M. Paul Jan 14 '17 at 04:58
0

Ohk I got it. preceded the second line by 4 spaces and then gave the statements of the block code. followed the same for the next line and then a blank line at the last. Thanks Anomitra, i got it just now.!!! Cheers.!! I am posting the block code for others who could get confused like me..

c=5 while c != 0: ...(space)(space)(space)(space)print(c) ...(space)(space)(space)(space)c -= 1 ...(blank line) 5 4 3 2 1

  • It is not mandatory to use 4 spaces. Check out the [first](https://docs.python.org/2.0/ref/indentation.html) example. Though it works as expected, the code is horrible to read and understand. Then, why 4 spaces? SO discussion [here](https://stackoverflow.com/a/1125719/3278350) – talekeDskobeDa Jan 15 '19 at 14:05