Indentation problems

Rhodri James rhodri at wildebst.demon.co.uk
Mon Nov 9 19:44:45 EST 2009


I'm going to make a whole bunch of wild guesses here, since you don't give  
us a lot to go on.

Wild Guess #1: you're using IDLE.

On Sun, 08 Nov 2009 19:01:37 -0000, Ray Holt <mrholtsr at sbcglobal.net>  
wrote:

> I am having problems with indentation some times. When I hit the enter  
> key
> after if statements or while statemt there are times when the  
> indentation is
> too much

Wild Guess #2: you've forgotten to close a bracket of some sort.

> and other times too little.

Wild Guess #3: you've forgotten the colon on the end of the line

> When I try to manually make sure the
> indentation is correct and try to print, I ge the error message of  
> invalid
> syntax or incorrect indentation.

Ah.  Wild Guess 1a: you're using IDLE in interactive mode.

In that case, bear in mind Wild Guesses 2 and 3, but they aren't the whole  
story.  In interactive mode, IDLE executes the code that you type Right  
Now This Instant And No Messing.  When you type a compound statement like  
"if" or "while", that presents a bit of a problem since the statement  
isn't really finished until you've typed in all the statements that belong  
to that "if" or "while".  Knowing this, IDLE puts off executing it, and  
helpfully adds the indentation that it knows you'll need.  If you fiddle  
with that extra space and delete too much of it (easily done here), IDLE  
will tick you off for getting your indentation wrong.  If you hit Return  
without typing anything else, IDLE will do exactly the same thing since it  
knows Python requires you to have at least one statement inside the "if".   
Once you've typed that one statement, you can carry on typing more to your  
hearts content; at this point, IDLE treats a blank line as meaning "I'm  
done."  At this point it goes away and executes what you've typed Right  
Now This Instant And No Messing, and may end up complaining about  
something you got wrong three lines ago.

> Can someone help me. Also when I open the
> edit window instead of the shell the programs tend not to run. Help! Ray

Well, no.  Unlike the interactive window, typing into the edit window  
doesn't cause anything to be executed Right Now Etc Etc.  It doesn't even  
cause anything to be executed Sometime Soon Honest Guv'nor.  It just saves  
up what you've done so that it can be run later, assuming you remember to  
save it to a file.  Unlike the interactive window, you can go back and  
change what you've written earlier to correct a mistake, and re-run the  
entire script rather than type it in again line by line.

To actually run your program you have two alternatives.  Either you can  
use the "Run Module" entry in the "Run" menu (F5 on my version: it may be  
called something slightly different in a slightly different menu with a  
slightly different shortcut key depending on your operating system and  
which version of IDLE you're running), or you can pull up a console  
(command line, terminal, xterm, whatever your OS calls it) and invoke  
Python on your file directly.  If that last bit didn't make any sense to  
you, don't worry, just leave that particular adventure in computing for  
another day.

-- 
Rhodri James *-* Wildebeest Herder to the Masses



More information about the Python-list mailing list