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()
Unable to find thre logic errror
Page 1 of 19 Replies - 1009 Views - Last Post: 18 September 2015 - 05:12 AM
#1
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.
Replies To: Unable to find thre logic errror
#2
Re: Unable to find thre logic errror
Posted 15 September 2015 - 05:21 PM
The newline character is \n, not /n.
#3
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().
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.
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
#4
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)
#5
Re: Unable to find thre logic errror
Posted 16 September 2015 - 04:49 AM
andrewsw, 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().
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.
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]
baavgai, 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?
#6
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.
#7
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
#8
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.."
"Return a list of the words in the string.."
This post has been edited by andrewsw: 16 September 2015 - 09:19 AM
#9
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.
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.
#10
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
Page 1 of 1

New Topic/Question
Reply


MultiQuote



|