Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python-shell on linux system indentation error [duplicate]

Tags:

python

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?

like image 637
Diganto M. Paul Avatar asked Oct 18 '25 16:10

Diganto M. Paul


2 Answers

You need to indent your loop body

word=("cheese") 
for character in word:
       print(character) 
like image 78
Yousaf Avatar answered Oct 21 '25 05:10

Yousaf


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
>>>
like image 27
Diganto M. Paul Avatar answered Oct 21 '25 05:10

Diganto M. Paul