0

I want to access the value of the term 'just' but somehow it gives the error: 'NoneType' object is not subscriptable. How can I solve this

def count_words(word_list):
    
    "Count word frequencies of words in a list."
    
    cnt_dict = dict()
    
    for word in word_list:
        if word in cnt_dict:
            cnt_dict[word] += 1
        else:
            cnt_dict[word] = 1
    print(cnt_dict)
  
my_words = ['this', 'is', 'just',
            'a', 'test', 'example', 'to',
           'test', 'some', 'example', 'code']
cnt = count_words(my_words)
cnt['just']
Thomas
  • 9
  • 2
  • 1
    You are not returning anything in your function `count_words`. Try replacing `print(cnt_dict)` with `return cnt_dict`. – j1-lee Oct 20 '22 at 19:08

2 Answers2

0

You need to return the dict at the end of your function. End your function with return cnt_dict.

jprebys
  • 2,469
  • 1
  • 11
  • 16
0

It works!

def count_words(word_list):
    
    "Count word frequencies of words in a list."
    
    cnt_dict = dict()
    
    for word in word_list:
        if word in cnt_dict:
            cnt_dict[word] += 1
        else:
            cnt_dict[word] = 1
    return cnt_dict
    
  
my_words = ['this', 'is', 'just',
            'a', 'test', 'example', 'to',
           'test', 'some', 'example', 'code']
cnt = count_words(my_words)
print(cnt["just"])
S3emingly
  • 15
  • 5