Why is my python word counting program not working? - python

i'm doing a python course and one of the questions asks me to write a program that counts words like this:
Enter line: which witch
Enter line: is which
Enter line:
is 1
which 2
witch 1
The code i have so far is:
occurences = {}
line = input('Enter line: ')
while line:
m = line.split()
for i in m:
if i in occurences:
occurences[i] += 1
else:
occurences[i] = 1
line = input('Enter line: ')
for word in sorted(occurences):
print(word, occurences[word])
But when I run this code, it either tells me every word occured only once or some other strange output. Thanks for the help!
Here is an example of it not working:
Enter line: test test test
Enter line: one two test
Enter line: one
Enter line:
test 3
This is the output i get while the expected output is:
test 4
two 1
one 2

Your input inside for loop is causing the problem, please try this:
occurences = {}
line = input('Enter line: ')
while line:
m = line.split()
print( m)
for i in m:
if i in occurences:
occurences[i] += 1
else:
occurences[i] = 1
print(occurences)
line = input('Enter line: ')
for word in sorted(occurences):
print(word, occurences[word])
By resetting the input inside the for loop, you are asking for new input after the first word is counted, and ignoring the subsequent words in the string.

When i ran the code i got no output at all, this is because you have a infinite loop by asking the user for another line at the end of the "While" loop, which cause it to do indefinitely.
To match what you were doing before I've changed your code a tiny bit
occurences = {}
line = input('Enter line: ')
while line:
m = line.split()
for i in m:
if i in occurences:
occurences[i] += 1
else:
occurences[i] = 1
break ## Once the word has been put into occurences it breaks
## so that the next loop can run
for word in sorted(occurences):
print(word, occurences[word])
if you want it to go forever until the user quits the program then you would do this:
while True:
occurences = {}
line = input('Enter line: ')
while line:
m = line.split()
for i in m:
if i in occurences:
occurences[i] += 1
else:
occurences[i] = 1
break
for word in sorted(occurences):
print(word, occurences[word])

Related

why is my code returning 0 even though word exists in file

So this is a code of me trying to find a word a user inputs and look up how many lines contain the word and if no lines contain the word output not found however when i input a word that I know exist in the file it returns 0 and not only is the word in the file it doesn't even output not found like I want it to. (here is my code)
response = input('Please enter words: ')
letters = response.split()
count = 0
with open("alice.txt", "r", encoding="utf-8") as program:
for line in program:
if letters in line:
count += 1
if(count < 1):
print("not found")
print(count)
What you're doing isn't gonna work the split function returns a list of strings and you're checking that list against a single string.
Is this what you wanted to do?
response = input("Please enter a word: ")
count = 0
with open("alice.txt", 'r') as program:
for line in program:
if response in line:
count += 1
if count == 0:
print("not found")
print(count)
You dont need the split function and the place of if condition is wrong in your code. Please refer below code.
response = input('Please enter word: ')
count = 0
with open("alice.txt", "r", encoding="utf-8") as program:
for line in program:
if response in line:
count += 1
if count == 0:
print('Not found')
else:
print(count)
You had an issue with opening the txt file as a single line, and not as a list of the individual lines.
Adding ".readlines()" can fix this issue!
I also went ahead and set the individual lines as 'line', where I then search for the input word in the new 'line' variable.
response = input('Please enter words: ')
letters = response.split()
count = 0
foo = open(
"alice.txt", "r",
encoding="utf-8").readlines()
for line in foo:
for word in letters:
if word in line:
count += 1
if(count < 1):
print("not found")
else:
print(count)

How to convert this while loop to for loop

Hello, I am fairly new to python and wondering how to convert this while loop to a for loop. I am not sure how to keep looping the inputs so it keeps asking Enter a line until GO and then say Next until STOP then display the count of lines.
def keyboard():
"""Repeatedly reads lines from standard input until a line is read that
begins with the 2 characters "GO". Then a prompt Next: , until a line is
read that begins with the 4 characters "STOP". The program then prints a
count of the number of lines between the GO line and the STOP line (but
not including them in the count) and exits."""
line = input("Enter a line: ")
flag = False
count = 0
while(line[:4] != "STOP"):
if (line[:2] == "GO"):
flag = True
if flag:
line = input("Next: ")
count += 1
else:
line = input("Enter a line: ")
print(f"Counted {count-1} lines")
keyboard()
With these inputs:
ignore me
and me
GO GO GO!
I'm an important line
So am I
Me too!
STOP now please
I shouldn't even be read let alone printed
Nor me
Should display/result in:
Enter a line: ignore me
Enter a line: and me
Enter a line: GO GO GO!
Next: I'm an important line
Next: So am I
Next: Me too!
Next: STOP now please
Counted 3 lines
You just need an infinity for loop which is what while True essentially is.
This answer has it: Infinite for loops possible in Python?
#int will never get to 1
for _ in iter(int, 1):
pass
So just replace your while loop with the above and add a break condition.
def keyboard():
"""Repeatedly reads lines from standard input until a line is read that
begins with the 2 characters "GO". Then a prompt Next: , until a line is
read that begins with the 4 characters "STOP". The program then prints a
count of the number of lines between the GO line and the STOP line (but
not including them in the count) and exits."""
line = input("Enter a line: ")
flag = False
count = 0
for _ in iter(int, 1):
if line[:4] == "STOP":
break
if (line[:2] == "GO"):
flag = True
if flag:
line = input("Next: ")
count += 1
else:
line = input("Enter a line: ")
print(f"Counted {count-1} lines")
keyboard()

Counting several instances of the same word from a text file

Complete beginner, searched a lot of threads but couldn't find a solution that fits me.
I have a text file, python_examples.txt which contains some words. On line four, the word hello appears twice in a row, like "hello hello".
My code is supposed to find the word the user inputs and count how many times it appears, it works but as I said, not if the same word appears multiple times on the same row. So there are 2 hellos on line 4 and one on line 13 but it only finds a total of 2 hellos. Fixes? Thanks,
user_input = input("Type in the word you are searching for: ")
word_count = 0
line_count = 0
with open ("python_example.txt", "r+") as f:
for line in f:
line_count += 1
if user_input in line:
word_count += 1
print("found " + user_input + " on line " + str(line_count))
else:
print ("nothing on line " + str(line_count))
print ("\nfound a total of " + str(word_count) + " words containing " + "'" + user_input + "'")
you can use str.count:
word_count += line.count(user_input)
instead of :
word_count += 1
it will count all appearance of user_input in the file line
The issue is with these two lines:
if user_input in line:
word_count += 1
You increase the count by 1 if the input appears on the line, regardless of whether it appears more than once.
This should do the job:
user_input = input("Type in the word you are searching for: ")
word_count = 0
with open("python_example.txt") as f:
for line_num, line in enumerate(f, start=1):
line_inp_count = line.count(user_input)
if line_inp_count:
word_count += line_inp_count
print(f"input {user_input} appears {line_inp_count} time(s) on line {line_num}")
else:
print(f"nothing on line {line_num}")
print(f"the input appeared a total of {word_count} times in {line_num} lines.")
Let me know if you have any questions :)
One option is use a library to parse the words in your text file rather than iterating one line at a time. There are several classes in nltk.tokenize which are easy to use.
import nltk.tokenize.regexp
def count_word_in_file(filepath, word):
"""Give the number for times word appears in text at filepath."""
tokenizer = nltk.tokenize.regexp.WordPunctTokenizer()
with open(filepath) as f:
tokens = tokenizer.tokenize(f.read())
return tokens.count(word)
This handles awkward cases like the substring 'hell' appearing in 'hello' as mentioned in a comment, and is also a route towards case-insenstive matching, stemming, and other refinements.

Word counter (Introduction to Programming 2(python))

I tried looking for an answer to this problem but it didn't work. (btw I am 15 and basically just started python)
This is my program:
all = []
count = {}
line = input("Enter line: ")
while line:
word = line.split()
line = input("Enter line: ")
for w in word:
if w in count:
count[w] += 1
else:
count[w] = 1
for word in sorted(count):
print(word, count[word])
This is the answer I'm getting:
Enter line: which witch
Enter line: is which
Enter line:
is 1
which 1
When I should be getting:
Enter line: which witch
Enter line: is which
Enter line:
is 1
which 2
witch 1
Please, can someone help?
for w in word: loop is executed outside while line: loop. It means that first you produce single word list for each line and then you skip each such list end executes for w in word: only for last word list.
To fix it change indentation of whoole for w in word: loop (this line and 4 lines below). Move it 4 spaces to the right.
all = []
count = {}
line = input("Enter line: ")
while line:
print("line is :" + line )
word = line.split()
line = input("Enter line: ")
for w in word:
if w in count:
count[w] += 1
else:
count[w] = 1
for word in sorted(count):
print(word, count[word])
It's because indentation in python is used to determine grouping of statements.
Here is way using Counter from collections module, it will give a dict with list elements as key at its frequency as values, it may help you to reduce the code length and use of for loop, You can get more idea about Counter from here
:
from collections import Counter
all = []
line = input("Enter line: ")
while line:
all.extend(line.split())
line = input("Enter line : ")
for i,j in Counter(all).items():
print(i, j)

Word Frequency Counter Python

Struggling with this exercise which must use a dictionary and count the number of times each word appears in a number of user inputs. It works in a fashion, but does not atomise each word from each line of user input. So instead of counting an input of 'happy days' as 1 x happy and 1 x days, it gives me 1 x happy days. I have tried split() along with the lower() but this converts the input to a list and I am struggling with then pouring that list into a dictionary.
As you may have guessed, I'm a bit of a novice, so all help would be greatly appreciated!
occurrences = {}
while True:
word = input('Enter line: ')
word = word.lower() #this is also where I have tried a split()
if word =='':
break
occurrences[word]=occurrences.get(word,0)+1
for word in (occurrences):
print(word, occurrences[word])
EDIT
Cheers for responses. This ended up being the final solution. They weren't worried about case and wanted the final results sorted().
occurrences = {}
while True:
words = input('Enter line: ')
if words =='':
break
for word in words.split():
occurrences[word]=occurrences.get(word,0)+1
for word in sorted(occurrences):
print(word, occurrences[word])
What you have is almost there, you just want to loop over the words when adding them to the dict
occurrences = {}
while True:
words = input('Enter line: ')
words = words.lower() #this is also where I have tried a split()
if words =='':
break
for word in words.split():
occurrences[word]=occurrences.get(word,0)+1
for word in (occurrences):
print(word, occurrences[word])
This line does not get executed: occurrences[word]=occurrences.get(word,0)+1
Because if it enters the if, it goes to the break and never executes that line. To make it be outside of the if don't indent it.
In general, the indentation of the posted code is messed up, I guess it's not really like that in your actual code.
Do you want line by line stats or do you want overall stats ? I'm guessing you want line by line, but you can also get overall stats easily by uncommenting a few lines in the following code:
# occurrences = dict() # create a dictionary here if yuo want to have incremental overall stats
while True:
words = input('Enter line: ')
if words =='':
break
word_list = words.lower().split()
print word_list
occurrences = dict() # create a dict here if you want line by line stats
for word in word_list:
occurrences[word] = occurrences.get(word,0)+1
## use the lines bellow if you want line by line stats
for k,v in occurrences.items():
print k, " X ", v
## use the lines bellow if you want overall stats
# for k,v in occurrences.items():
# print k, " X ", v

Categories