Hi, everyone, I was going through the python for everybody course and when I reached video 23, I tried making a word counter in a text you enter but for some reason it doesn’t work, whenever I enter the text python shuts down.
here is my code
counts = dict()
text = input('enter text')
text = text.strip
words = text.split()
for word in words:
counts[word] = counts.get(word, 0) + 1
bigcount = None
bigword = None
for word,count in counts.items:
if bigcount == None :
bigcount = count
bigword = word
else :
bigcount = count
bigword = word
print(counts.items)
import time
time.sleep(5)
counts = dict()
text = input('enter text')
text = text.strip()
words = text.split()
for word in words:
counts[word] = counts.get(word, 0) + 1
bigcount = None
bigword = None
for word,count in counts.items:
if bigcount == None :
bigcount = count
bigword = word
else :
bigcount = count
bigword = word
print(counts.items)
import time
time.sleep(5)
You defined text as a file handler
You cannot use .strip on a file handler , you can only use it on a string
That’s why you put it after you select a line from the file as in :
for line in text : #at this point: line is a string containing the first line in the file you chose. #Therefore you can use .strip and .split on it.
line=line.strip()
words=line.split() #at this point we used .split on line the result is words: a list.
for word in words :
counts[word] = counts.get(word, 0) + 1