Hello,
I am new to python and i am having problems in the output format of "for" "in" loop
example the code below

# A word count programme
# Author why jay
def main():
    import string
    word=0
    line=0
    letters=0
    asd=raw_input("Enter file here : ")
    f_name=open(asd,'r')
    for why in f_name:
        see=why.split()

        line=line+1
        word=word+len(see)
        letters=letters+len(why)
        f_name.close()
        print "Number of lines,words,letters is : " ,line,word,letters


main()

the above programme gives the output as thus:
Number of lines,words,letters is :  1 1 9

Number of lines,words,letters is : 2 2 16
Number of lines,words,letters is : 3 3 25
Number of lines,words,letters is : 4 4 36
Number of lines,words,letters is : 5 6 50
Number of lines,words,letters is : 6 7 56
Number of lines,words,letters is : 7 8 68
Number of lines,words,letters is : 8 9 75
Number of lines,words,letters is : 9 11 90
Number of lines,words,letters is : 10 12 101
Number of lines,words,letters is : 11 12 102
Number of lines,words,letters is : 12 13 106
Number of lines,words,letters is : 13 14 112
Number of lines,words,letters is : 14 15 128

please someone help me to output of the code as
Number of lines,words,letters is : 14 15 128
I would will be really gratefull if someone could help me.Thanks in advance

thou, I don't really see where's the problem?

Number of lines,words,letters is : 14 15 128

please someone help me to output of the code as
Number of lines,words,letters is : 14 15 128

Isn't this the given output?

Something like:

# A word count programme
# Author why jay
def main():
    word = line = letters = 0
    f_name=raw_input("Enter file here : ")
    with open(f_name ,'r') as f:
        for line, why in enumerate(f, 1):
            word += len(why.split())
            letters += len(why)
    print "Number of lines,words,letters is : " ,line,word,letters


main()

I've look uppon your code, and I have to add something:
In Python, as a programming language, the indentation is the only thing that keeps the script/file together. So, You must indend only as needed, nothing more, or less.:
1st of all

   f_name.close()
    print "Number of lines,words,letters is : " ,line,word,letters

inside the loop will rruin the program. f_name.close() will close the file, and on a 2nd attempt on passing the for argument it will give an error, because the file is close.
the print funtion will print everytime, so you'll have to get it out of the for
here's the code revised, just tell me if you have further questions:

def main():
#    import string
    word = 0
    line = 0
    letters = 0
    asd = raw_input("Enter file here : ")
    f_name = open(asd, 'r')
    for why in f_name:
        see = why.split()
        line = line + 1
        word = word + len(see)
        letters = letters + len(why)

    print "Number of lines,words,letters is : " , line, word, letters
    f_name.close()
main()

Do mean something like this:

line = 14 
word = 15 
letters = 128

print "Number of lines,words,letters is : " ,line,word,letters
# using the C type  % specifier
print "Number of lines,words,letters is :  %d %d %d" % (line,word,letters)
# include justification
print "Number of lines,words,letters is :  %5d %5d %5d" % (line,word,letters)

'''my result -->
Number of lines,words,letters is :  14 15 128
Number of lines,words,letters is :  14 15 128
Number of lines,words,letters is :     14    15   128
'''

Edited 4 Years Ago by HiHe

This article has been dead for over six months. Start a new discussion instead.