[Tutor] Question on a example in the book "Learn to program usingpython" by Alan Gauld

Alan Gauld alan.gauld at blueyonder.co.uk
Sun Aug 1 20:06:31 CEST 2004


Hi Klas,


> I got stuck on the case studie for the very useful :) program
"Grammar
> counter" though.

I am currently reworking the Grammer checker as part of my rewrite of
the web tutorial. I've never been too happy with that case study
because
I tried to show a real world example of a program evolving warts and
all. The end result (as is often the case!) was a bit of a messy,
sub optimal program. What I should have done and didn't was added
a section showing how to take the working version and tidy it up!

> No matter what i try i get the error "Error analyzing file....."

But despite what I just said, it should work! :-)
But as you find it in the book it doesn't, due to a silly mistake
- Sorry !

I missed out self from a couple of attribute accesses:

>     def generateStats(self):
>         self.word_count = len(self.groups)
>         for c in self.stop_tokens:
>             sentence_count = sentence_count + \
>                              self.punctuation_counts[c]
>         for c in self.punctuation_counts.keys():
>             clause_count = clause_count + \
>                            self.punctuation_counts[c]

both sentence_count and clause_count are attributes of the class
so should be accessed via self.sentence_count etc...

This error was picked up by a reader and the fix is listed on the book
errata page, which you should check - there are quite a lot of typos,
not
all of them my fault!.

http://www.freenetpages.co.uk/hp/alan.gauld/book/errata.htm

HTH.

FWIW The Games Framework/Hangman case study is much better quality
code because it illustrates designing a program from scratch rather
than
evolving an existing one.

Alan G.



More information about the Tutor mailing list