Re: Indentation Errors
[Originally posted by d. harms] Hi Keith, You appear to be getting caught with two behaviors the interactive mode exhibits with compound statemts (like an if/elif/else statement) that can certainly be confusing when you first encounter them. The first is that for a top level statement you want your subsequent clauses to start flush against the left side, i.e.: >>> if x < 5: y = 1 else: y = 1 >>> You have this right later in your session, but then are running into the second behavior. In the interactive mode you need to put in an extra carriage return (i.e. hit the enter key again) when you want to end a top level compound statement and get the ('>>>') prompt back before entering another top level statement. Thus in this example you want to get the prompt back before entering the print statement, i.e.: Python 2.2.1 (#34, Apr 9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32 Type "copyright", "credits" or "license" for more information. IDLE 0.8  press F1 for help >>> x=5 >>> if x<5: y=1 z=5 elif x>5: y=1 z=11 else: y=0 z=10 >>> print x,y,z 5 0 10 >>> After entring z=10, I had to hit enter twice to get the prompt back. Daryl > I am having trouble enterin the code on page 36 of The Quick Python Book. In > both 4.4.2 and 4.4.3, when I enter the code interactively, I get numerous > indentation errors. I suspect this has to do with the '>>>' prompt string, but > have tried several different indentations and continue to get errors. > > Can someone tell me what I'm doing wrong? Thanks. (My interactive session is > attached below.) > > Keith > > Python 2.2.1 (#34, Apr 9 2002, 19:34:33) [MSC 32 bit (Intel)] on win32 > Type "copyright", "credits" or "license" for more information. > IDLE 0.8  press F1 for help > >>> x=5 > >>> if x<5: > y=1 > z=5 > elif x>5: > > IndentationError: unindent does not match any outer indentation level (line 4) > >>> if x<5: > y=1 > z=5 > elif x>5: > > IndentationError: unindent does not match any outer indentation level (line 4) > >>> if x<5: > y=1 > z=5 > elif x>5: > > SyntaxError: invalid syntax > >>> if x<5: > y=1 > z=5 > elif x>5: > y=1 > z=11 > else: > y=0 > z=10 > print x, y, z > SyntaxError: invalid syntax > >>> if x<5: > y=1 > z=5 > elif x>5: > y=1 > z=11 > else: > y=0 > z=10 > print x, y, z > SyntaxError: invalid syntax > >>> if x<5: > y=1 > z=5 > elif x>5: > y=1 > z=11 > else: > y=0 > z=10 > x, y, z > SyntaxError: invalid syntax > >>> if x<5: > y=1 > z=5 > elif x>5: > y=1 > z=11 > else: > y=0 > z=10 > x > SyntaxError: invalid syntax > >>> u,v,x,y=0,0,100,30 > >>> while x>y: > u=u+y > x=xy > if x<y+2:> > v=v+x > x=0 > else: > v=v+y+2 > x=xy2 > print u, v > SyntaxError: invalid syntax > >>> while x>y: > u=u+y > x=xy > if x<y+2:> > v=v+x > x=0 > else: > v=v+y+2 > x=xy2 > print u, v > > IndentationError: unindent does not match any outer indentation level (line 10) > >>> while x>y: > u=u+y > x=xy > if x<y+2:> > v=v+x > x=0 > else: > v=v+y+2 > x=xy2 > print u, v > > IndentationError: unindent does not match any outer indentation level (line 10) > >>>
