Python for everybody

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)

THX in advance :slight_smile:
Edit: I am using python 3.9

I think you are missing something here

just edited it doesn’t work yet

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)

What happens when you run it?

it asks for the text and when i enter it , it just shuts down

You are also not calling items

I would suggest you work on debugging, add a print statement between each line, find out where it stops working

Aren’t you getting any error? Python is pretty good at telling you if something is not correct

If the print statements not work, you can practice with a tool like https://pythontutor.com/

thx im looking into it now and no it doesnt show an error

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

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.