i am using python idle in windows7 to run my program.
whenever i try to run the program i get an syntax error as unexpected indent.
the code i am trying to run is as follows:

def print_lol(movies):
	for each in movies:
		if isinstance(each,list):
			print_lol(each)
		else:
                     print(each)

Recommended Answers

All 25 Replies

def print_lol(movies):
for each in movies:
if isinstance(each,list):
print_lol(each)
else:
print(each)

>>> def print_lol(movies):
	for each in movies:
		if isinstance(each,list):
			print_lol(each)
		else:
                     print(each)

This worked for me.
I wanted to ask that if movies is not a list here ( you are checking via isinstance ), then what are you iterating through? (maybe you have strings in that list movies).

Don't indent python code with tab characters. Configure your editor to insert 4 spaces instead of a tab when you hit the tab key. Also reindent your code like this

def print_lol(movies):
    for each in movies:
        if isinstance(each,list):
            print_lol(each)
        else:
            print(each)

I used the "reindent" script to reindent the code from the command line http://pypi.python.org/pypi/Reindent/0.1.0

Don't indent python code with tab characters. Configure your editor to insert 4 spaces instead of a tab when you hit the tab key. Also reindent your code like this

def print_lol(movies):
    for each in movies:
        if isinstance(each,list):
            print_lol(each)
        else:
            print(each)

I used the "reindent" script to reindent the code from the command line http://pypi.python.org/pypi/Reindent/0.1.0

thanks for reply, i am new to python programming.can you please clear the following:
what does it mean "Don't indent python code with tab characters"?
when you say configure your editor to insert 4 spaces instead of tab,what does it mean.
i have set the identation width to 4 spaces by going to options => configuring the idle.
and can you explain how you reindent the code from your command line.i have downloaded the zip file from the url given by you.after extracting i ran the reindent.py, but still i get the error.

>>> def print_lol(movies):
	for each in movies:
		if isinstance(each,list):
			print_lol(each)
		else:
                     print(each)

This worked for me.
I wanted to ask that if movies is not a list here ( you are checking via isinstance ), then what are you iterating through? (maybe you have strings in that list movies).

movies is a nested list conataining numbers.for eg.
movies=[12,23,[23,21,[2,42]],22]

thanks for reply, i am new to python programming.can you please clear the following:
what does it mean "Don't indent python code with tab characters"?
when you say configure your editor to insert 4 spaces instead of tab,what does it mean.
i have set the identation width to 4 spaces by going to options => configuring the idle.
and can you explain how you reindent the code from your command line.i have downloaded the zip file from the url given by you.after extracting i ran the reindent.py, but still i get the error.

If you extracted the zip file, open a cmd tool, navigate to the extracted folder and type python setup.py install . Then navigate to the folder containing your python script and type reindent nameofthescript.py . It should reindent your script (at least that's what happens in linux)...

Your initial post contains tab characters (\t) entered with the tab key by many editors. If you edit a program with idle, it should not indent with tabs.

If you extracted the zip file, open a cmd tool, navigate to the extracted folder and type python setup.py install . Then navigate to the folder containing your python script and type reindent nameofthescript.py . It should reindent your script (at least that's what happens in linux)...

Your initial post contains tab characters (\t) entered with the tab key by many editors. If you edit a program with idle, it should not indent with tabs.

after navigatigating to extracted folder from cmd,when i enter python setup.py install, i get the following error:


'python' is not recognized as an internal or external command,
operable program or batch file.

If you extracted the zip file, open a cmd tool, navigate to the extracted folder and type python setup.py install . Then navigate to the folder containing your python script and type reindent nameofthescript.py . It should reindent your script (at least that's what happens in linux)...

Your initial post contains tab characters (\t) entered with the tab key by many editors.
If you edit a program with idle, it should not indent with tabs.

if i put only setup.py install in cmd then i get following error:
Traceback (most recent call last):
File "C:\Users\harshal\Downloads\Reindent-
5, in <module>
from setuptools import setup
ImportError: No module named setuptools

If you are getting that error at cmd, then you have not installed python correctly, or most probably you haven't added python to your environment variables.
please look at: python docs

If you are getting that error at cmd, then you have not installed python correctly, or most probably you haven't added python to your environment variables.
please look at: python docs

i think that i have installed python ccorrectly as i am able to run other programs.i have not set python to environment variables, i will try it now.

If you are getting that error at cmd, then you have not installed python correctly, or most probably you haven't added python to your environment variables.
please look at: python docs

set up the environmental variables using following cmd:
set PYTHONPATH=%PYTHONPATH%;C:\Python32

but again it gives the same previous error after i put setup.py install in cmd

set up the environmental variables using following cmd:
set PYTHONPATH=%PYTHONPATH%;C:\Python32

but again it gives the same previous error after i put setup.py install in cmd

If you don't have setuptools, get it from here. If your windows is 32 bits, simply download the .exe for your version of python and run it. http://pypi.python.org/pypi/setuptools#downloads

If you don't have setuptools, get it from here. If your windows is 32 bits, simply download the .exe for your version of python and run it. http://pypi.python.org/pypi/setuptools#downloads

installed the setuptools correctly, then environmental variables set up correctly.After that as told downloaded the reindent zip file, extracted it, and then installed it correctly using cmd.

Now the only problem is in last step where you told to enter reindent nameofthescript.py in cmd after moving to script directory.I am getting following error:
'reindent' is not recognized as an internal or external command,
operable program or batch file.

installed the setuptools correctly, then environmental variables set up correctly.After that as told downloaded the reindent zip file, extracted it, and then installed it correctly using cmd.

Now the only problem is in last step where you told to enter reindent nameofthescript.py in cmd after moving to script directory.I am getting following error:
'reindent' is not recognized as an internal or external command,
operable program or batch file.

Try easy_install reindent first in a cmd tool (from any folder).

Try easy_install reindent first in a cmd tool (from any folder).

getting following error when doing above:
'easy_install' is not recognized as an internal or external command,
operable program or batch file.

getting following error when doing above:
'easy_install' is not recognized as an internal or external command,
operable program or batch file.

It could be a PATH problem. The easy_install documentation http://peak.telecommunity.com/DevCenter/EasyInstall#windows-notes says that C:\\Python2X\\Scripts must be in your path for easy_install to work (and perhaps reindent too ?) (replace Python2X by your own python folder)

It's worth having a working easy_install because you can install any package from pypi with it (something like 15000 packages) !

It could be a PATH problem. The easy_install documentation http://peak.telecommunity.com/DevCenter/EasyInstall#windows-notes says that C:\\Python2X\\Scripts must be in your path for easy_install to work (and perhaps reindent too ?) (replace Python2X by your own python folder)

It's worth having a working easy_install because you can install any package from pypi with it (something like 15000 packages) !

now easyInstall is also installed correctly, reindent is also working and after reindenting the prolis.py file again when i open the prolis.py file in idle and run module then i get following error:
expected an indented block.

now easyInstall is also installed correctly, reindent is also working and after reindenting the prolis.py file again when i open the prolis.py file in idle and run module then i get following error:
expected an indented block.

Can you post prolis.py (between code tags) ? Also python probably tells you the error's line number.

Can you post prolis.py (between code tags) ? Also python probably tells you the error's line number.

after reindenting the file is like this:

def print_lol(movies):
for each in movies:
if isinstance(each,list):
print_lol(each)
else:
print(each)

after reindenting the file is like this:

def print_lol(movies):
for each in movies:
if isinstance(each,list):
print_lol(each)
else:
print(each)

Well, I've never seen that. If it is not too long, reindent it by hand with idle, it should look like

def print_lol(movies):
    for each in movies:
        if isinstance(each,list):
            print_lol(each)
        else:
            print(each)

Well, I've never seen that. If it is not too long, reindent it by hand with idle, it should look like

def print_lol(movies):
    for each in movies:
        if isinstance(each,list):
            print_lol(each)
        else:
            print(each)

hey it works after i do it manually,and the script also runs correctly as expected but any ideas how should i perform the identation in future for larger programs.
Thanks again.

hey it works after i do it manually,and the script also runs correctly as expected but any ideas how should i perform the identation in future for larger programs.
Thanks again.

In the future, always use an editor where indentation is configured with 4 spaces (like your idle), and if you find a bad indentation in a forum post or in a 3rd party program, try to use "reindent" to clean the script.

In the future, always use an editor where indentation is configured with 4 spaces (like your idle), and if you find a bad indentation in a forum post or in a 3rd party program, try to use "reindent" to clean the script.

i was doing the same, i mean the idle i was using,was configured with 4 spaces by default.
So does it mean that i will have to indent the script manually everytime?

i was doing the same, i mean the idle i was using,was configured with 4 spaces by default.
So does it mean that i will have to indent the script manually everytime?

No, for some reason there was a tab character "\t" in your first post instead of 4 spaces " " .

Normally you don't have to reindent python code.

No, for some reason there was a tab character "\t" in your first post instead of 4 spaces " " .

Normally you don't have to reindent python code.

Ok thanks for helping.It was a long discussion.
Thanks again.

Ok thanks for helping.It was a long discussion.
Thanks again.

last thing, you may be interested in the standard module tabnanny (see info here http://effbot.org/librarybook/tabnanny.htm) and by the pep 8 advices http://www.python.org/dev/peps/pep-0008/ . Good luck.

Edit: you don't need to install tabnanny. It comes with your python distribution.

commented: very helping. +1
commented: thanks +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.