The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by keithr]

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=x-y
if x<y+2:>
v=v+x
x=0
else:
v=v+y+2
x=x-y-2
print u, v
SyntaxError: invalid syntax
>>> while x>y:
u=u+y
x=x-y
if x<y+2:>
v=v+x
x=0
else:
v=v+y+2
x=x-y-2
print u, v

IndentationError: unindent does not match any outer indentation level (line 10)
>>> while x>y:
u=u+y
x=x-y
if x<y+2:>
v=v+x
x=0
else:
v=v+y+2
x=x-y-2
print u, v

IndentationError: unindent does not match any outer indentation level (line 10)
>>>
import-bot (20211) [Avatar] Offline
#2
Page 36 Problem (Was: Re: Indentation Errors)
[Originally posted by keithr]

I checked the IDLE online documentation (thanks, Daryl) and discovered the
Backspace trick. (Interesting that backspacing from a single level of
indentation takes me back to the left margin rather than lining up the
statement below the beginning of the first statement, which is indented due to
the prompt.)

The "print" statement still gives me an error, though, so clearly this doesn't
work in interactive mode. Is there a way I can do this interactively and still
display the x, y, and z values?
import-bot (20211) [Avatar] Offline
#3
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=x-y
> if x<y+2:>
> v=v+x
> x=0
> else:
> v=v+y+2
> x=x-y-2
> print u, v
> SyntaxError: invalid syntax
> >>> while x>y:
> u=u+y
> x=x-y
> if x<y+2:>
> v=v+x
> x=0
> else:
> v=v+y+2
> x=x-y-2
> print u, v
>
> IndentationError: unindent does not match any outer indentation level (line
10)
> >>> while x>y:
> u=u+y
> x=x-y
> if x<y+2:>
> v=v+x
> x=0
> else:
> v=v+y+2
> x=x-y-2
> print u, v
>
> IndentationError: unindent does not match any outer indentation level (line
10)
> >>>