[Tutor] Hi All

alan.gauld@bt.com alan.gauld@bt.com
Mon, 26 Aug 2002 13:46:46 +0100


> Hi could one or 2 of you help me with this I have gotten 
> this far I havnt got the hang of python yet but am 
> working on it .

> Here it goes:

> def getStuff(file)
>     info = open(file,"r")
>     infolines = info.readlines()
>     for line in infolines:
>        word = string.split(lines)

Assuming the indentation wackiness is due to a 
cut n paste error (if not fix that first, Python 
is fussy about indentation). 'lines' does not 
exist here it should be 'line' - names must be 
consistent.

   print"Line %d contains %d words" % (linenum,len(word))
   linenum += 1

linenum does not exist you need to initialise it at the 
top of the function:

linenum = 1  # or zero?


'word' is a confusing name since its more than one word. 
Better would be to use words. These small changes make 
your code much easier to read.


   totalwords += len(words)

words does not exiost here nor does totalwords. You will 
need to use 'word'(or change it to 'words' in the code 
above) and initialize totalwords at the top of the function.

    print "Total lines:", linenum - 1
    print "Average words per line:", float(totalwords)/linenum - 1

You might want to use parens to disambiguate the calculation:

    print "Average words per line:", float(totalwords)/(linenum - 1)

Or better since you calculate the same thing twice reset the 
variable:

linenum = linenum -1
print "Total lines:", linenum
print "Average words per line:", float(totalwords)/linenum

HTH,

Alan G.