[Tutor] Major newbie question..
Kevin Liang
7kcsl@qlink.queensu.ca
Wed, 10 Nov 1999 02:50:42 -0500
This might seem like a lame question, but I'm following a tutorial on a web page, and its at a point where you're
supposed to create a program to count the words in a file. As you can
see, I'm a little stumped..any pointers/tips would be appreciated. The
thing runs, but not correctly. I thought that adding .inp=
resp.readlines(): would do the trick, but I guess not. Thanks in
advance
Kevin
import string
def numwords(s):
list = string.split(s) # need to qualify split() with string module
return len(list) # return number of elements in list
resp = raw_input("Enter a filename : ")
inp = resp
total = 0 # initialise to zero; also creates variable
for line in resp:
total = total + numwords(line) # accumulate totals for each line
print "File had %d words" % total