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]

As evidenced by my previous post, I am clearly having a problem
differentiating between interactive and non-interactive code in the text.

For example, the code at the top of page 183 (section 16.7) is identified as
interactive. For this to work, though, the classes first have to be defined.
However, the class definitions on page 182 are not identified as interactive.
And if I try to enter them interactively, I get errors. If I backspace after
entering the first "print" statement, I am taken back to the left margin, and
entering "class C(P):" gives me an error. On the other hand, if I enter an
extra <cr> after the first "print", to get the blank line shown in the text, I
am taken to a prompt and the first definition appears to be lost.

Can someone please explain to me how I am supposed to enter these examples?

Thanks.

Keith
import-bot (20211) [Avatar] Offline
#2
Re: Interactive vs. Non-Interactive
[Originally posted by d. harms]

Hi Keith,

Sorry, we are playing a little loose here between interactive and
non-interactive mode. You are likely getting caught with really the same
problem as you were in your previous post with regard to the if/elif/else
compound statement. The class definition is another compound statement.

Likely the best way to enter the example interactively in IDLE is to copy in
the first class definition. Hit enter to make sure you have a new top level
prompt. Then copy in the second and again make sure you get a top level prompt
before entering in the commands using them. I.e.:

>>> class P:
z = "Hello"
def setP(self):
self.x = "Class P"
def printP(self):
print self.x


>>> class C(P):
def setC(self):
self.x = "Class C"
def printC(self):
print self.x


>>> c = C()
>>> c.setP()
>>>

Note that in the above example I copied in the definitions (i.e. as obtained
from the source code zip file that you can download from this book's web page)
and thus the indentation exhibited is four spaces. If you type them directly
into IDLE you will get its default 8 space indentation which is less
confusing, i.e. :

>>> class P:
z = "Hello"
def setP(self):
self.x = "Class P"
def printP(self):
print self.x


>>>

Hope this helps you get through this.

Daryl


> As evidenced by my previous post, I am clearly having a problem
> differentiating between interactive and non-interactive code in the text.
>
> For example, the code at the top of page 183 (section 16.7) is identified as
> interactive. For this to work, though, the classes first have to be defined.
> However, the class definitions on page 182 are not identified as interactive.
> And if I try to enter them interactively, I get errors. If I backspace after
> entering the first "print" statement, I am taken back to the left margin, and
> entering "class C(P):" gives me an error. On the other hand, if I enter an
> extra <cr> after the first "print", to get the blank line shown in the text, I
> am taken to a prompt and the first definition appears to be lost.
>
> Can someone please explain to me how I am supposed to enter these examples?
>
> Thanks.
>
> Keith
import-bot (20211) [Avatar] Offline
#3
Re: Interactive vs. Non-Interactive
[Originally posted by keithr]

Thanks, Daryl. All problems solved now!

I typed the code in directly in all cases, making sure I got the '>>>' prompt
back each time (I guess I was afraid I was going to learn my assignments the
first time through), and everything worked properly.

Thanks for the replies!

Keith