9 Replies - 1009 Views - Last Post: 18 September 2015 - 05:12 AM Rate Topic: -----

#1 matttm   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 04-September 15

Unable to find thre logic errror

Posted 15 September 2015 - 05:18 PM

I am a newbie to working with files in npython, and I am trying to create a word counter. My program returns a count oi 8 even though it should've yielded 6. Leaving me to deduce that the program is still counting white space.

def countWords(txt_file):
    wordCnt = 0
    for line in txt_file:
        line = line.strip()
        word = line[0:]

        if (line == ' ' or line == '  ' or line == '/n'):
            print(line)
        else:
            wordCnt += 1
    return wordCnt

def main():

    txt_file = input("Enter text file name with its extension: ")
    open(txt_file)

    wordCnt = countWords(txt_file)
    if wordCnt == 1:
        print("There is", wordCnt,"word in ", txt_file, ".")
    else:
        print("There are", wordCnt,"words in ", txt_file, ".")
    input("Press enter to finish.")

    # txt_file.close()

main()



Is This A Good Question/Topic? 0
  • +

Replies To: Unable to find thre logic errror

#2 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: Unable to find thre logic errror

Posted 15 September 2015 - 05:21 PM

The newline character is \n, not /n.
Was This Post Helpful? 1
  • +
  • -

#3 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Unable to find thre logic errror

Posted 15 September 2015 - 11:31 PM

Your code is just counting the number of lines in the file:

word = line[0:] This just copies a line, it is not splitting it into words, but you do nothing with word anyway. If your intention was to split a sentence into words you would use split().

        if (line == ' ' or line == '  ' or line == '\n'):

You've stripped line of leading and trailing whitespace so it won't consist of just whitespace characters; that is, this if condition will never be met. So the else is always executing, so it just counts the number of lines that you are iterating through.

This post has been edited by andrewsw: 15 September 2015 - 11:34 PM

Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is offline

  • Dreaming Coder
  • member icon


Reputation: 7507
  • View blog
  • Posts: 15,558
  • Joined: 16-October 07

Re: Unable to find thre logic errror

Posted 16 September 2015 - 04:19 AM

# this is meaningless
open(txt_file)
# you've just opened a file
# with the name txt_file
# and the function has returned
# a file object
# which you just totally ignored

# you are now counting the words in your filename
wordCnt = countWords(txt_file)


Was This Post Helpful? 2
  • +
  • -

#5 matttm   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 04-September 15

Re: Unable to find thre logic errror

Posted 16 September 2015 - 04:49 AM

View Postandrewsw, on 15 September 2015 - 11:31 PM, said:

Your code is just counting the number of lines in the file:

word = line[0:] This just copies a line, it is not splitting it into words, but you do nothing with word anyway. If your intention was to split a sentence into words you would use split().

        if (line == ' ' or line == '  ' or line == '\n'):

You've stripped line of leading and trailing whitespace so it won't consist of just whitespace characters; that is, this if condition will never be met. So the else is always executing, so it just counts the number of lines that you are iterating through.


I thought that the if was doing something because there is still spaces between words, unless I understood it wrong, and it removes head and trail spaces for each word.
[/quote]


View Postbaavgai, on 16 September 2015 - 04:19 AM, said:

# this is meaningless
open(txt_file)
# you've just opened a file
# with the name txt_file
# and the function has returned
# a file object
# which you just totally ignored

# you are now counting the words in your filename
wordCnt = countWords(txt_file)




I am opening the file though (atleast I think). I am storing the file's name in a variable and opening the variable. So, isn't it opening that file?
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Unable to find thre logic errror

Posted 16 September 2015 - 05:09 AM

Quote

..and opening the variable.

Re-read baavgai 's post. You open the file, but don't do anything with the reference to this file. If the filename is "bob" then it is just the word "bob" that you are passing to your function.

I think you are just counting the number of characters in the filename (txt_file), excluding spaces.

Once you've fixed this then re-read my post.
Was This Post Helpful? 0
  • +
  • -

#7 matttm   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 04-September 15

Re: Unable to find thre logic errror

Posted 16 September 2015 - 07:48 AM

def countWords(txt_file):
    wordCnt = 0
    for line in txt_file:
        line = line.strip()

        print(line)
        wordCnt += 1
    return wordCnt

def main():

    txt_file = open(input("Enter text file name with its extension: "))


    wordCnt = countWords(txt_file)
    if wordCnt == 1:
        print("There is", wordCnt,"word in ", txt_file, ".")
    else:
        print("There are", wordCnt,"words in ", txt_file, ".")
    input("Press enter to finish.")

    # txt_file.close()

main()


I saw what baavgai was saying and fixed it because it is being printed in the console. The problem is now, it only counts lines, and I don't know how I can specify groups of characters between the spaces. If I could somehow assign a group like this to a var, I could use the counter. Can I use "for word in line"?


ie
for word in line
    wordCnt += 1

Was This Post Helpful? 0
  • +
  • -

#8 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Re: Unable to find thre logic errror

Posted 16 September 2015 - 09:18 AM

str.split() :the docs

"Return a list of the words in the string.."

This post has been edited by andrewsw: 16 September 2015 - 09:19 AM

Was This Post Helpful? 0
  • +
  • -

#9 matttm   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 04-September 15

Re: Unable to find thre logic errror

Posted 16 September 2015 - 06:02 PM

Ok, is there a way without usingthat function.

I am told:
By removing leading and trailing whitespace, counting the number of whitespace blocks and
then adding 1, we get the number of words in one line of text.

I did this by doing a line sttip, but I don't know how to count the spaces.
Was This Post Helpful? 0
  • +
  • -

#10 matttm   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 30
  • Joined: 04-September 15

Re: Unable to find thre logic errror

Posted 18 September 2015 - 05:12 AM

Alright, Im a hell of alot closer to solving this the way the teacher wants. It says though, that the index is out of range.
def strHead(st):
    #returns first character
   return st[0]

def strTail(st):
    #returns all but initial characters
   return st[1:]

def countWords(txt_file):
    #counts words in txt file
    wordCnt = 0
    for line in txt_file.read():
        i = 1
        line = line.strip()
        head = strHead(line)

        while head >= 'a' or head <= 'Z':
            head = strHead(strTail(line[i:]))
            i += 1
        else:
            wordCnt += 1
            # head = strHead(strTail())
    return wordCnt


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1