Hi all,

New to the forum here, and coding in general. I am posting because I am having a problem with a simple program intended to act as a word, character and line counter, with each counter being its own function.

I keep running into the same problem over and over. Everything works fine if I don't break them out into separate functions. But when I break them into individual functions, something breaks. If I call the linecount function first, then it will work fine, but the other two return a value of 0. If I call the other two first, then the linecount function will will return a value of 0.

I am sure something is happening as variables are passed from the main function to linecount, but I can't seem to understand why it's changing. My understanding is that passing a variable into a function as a parameter wouldn't alter the original value (it's only modified locally within the local function).

Anyway, here is what I have. I removed most of the comments so it was shorter and quicker for people to read. If that was a bad move (again, I am a complete beginner) then let me know and i will post the commented version.

I am not looking for someone to fix my code, as much as help me understand what I am missing.

def linecount(x):
    lines = 0
    for line in x:
        lines += 1
    return lines

def wordcount(wc_target):
    split_words = wc_target.split(None)     
    words = len(split_words)                
    return words                            

def charactercount(cc_target):
    characters = 0
    for number in cc_target:
        characters += 1
    return characters

def main():

    print """
    This program will evaluate a text (.txt) file, count the number
    of lines, words, and total characters in the file you enter then
    display each value.

    filename = raw_input("Please enter the name of the file you would like to count: ")
    readfile = open(filename)
    listdata = readfile.read()

    print "For the file %s, here are your values:" % filename
    print "Lines     :", linecount(readfile)
    print "Words     :", wordcount(listdata)
    print "Characters:", charactercount(listdata)


thanks for any thoughts, guidance!

Actually quite simple. After readfile.read() seek points to end of the file. You can reset seek to the beginning of the file by inserting:
right ofter line:
listdata = readfile.read()

That was exactly the problem. I didn't realize it would read and then point to the end of the file! I was trying different things for hours trying to get it sorted.

As a total beginner, I would never have thought to do something like that.

Thanks for the help, that is one mistake I won't be making again.