Python Forum
Dictionary Homework - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Dictionary Homework (/thread-14677.html)



Dictionary Homework - beepBoop123 - Dec-11-2018

Hi, I keep getting an error that says "str' object has no attribute 'items" on line 3 and I don't understand why. Can someone help me fix the code? Thanks!

def get_inverted_index(words):
    tweetDict = {}
    wordCount = {}
    for tweetKey, tweetText in words.items():
        for word in tweetText.lower().split():
            wordCount[word]=wordCount.get(word,0)+1
            if inverted_index.get(word,False):
                if tweetKey not in tweetDict[word]:
                    tweetDict[word].append(tweetKey)
            else:
                tweetDict[word] = [tweetKey]
    return tweetDict, wordCount
gettysburg_address = "Four score and.."
print = (get_inverted_index(gettysburg_address))



RE: Dictionary Homework - buran - Dec-11-2018

gettysburg_address is a str, i.e. "Four score and.."
you need to pass dict as argument when call the function, not str


RE: Dictionary Homework - ichabod801 - Dec-11-2018

You are passing a string to the function on line 15. That string is in the variable words in the function. As the error says, strings don't have items. That's an attribute of a dictionary.

Sniped: buran beat me to it.


RE: Dictionary Homework - buran - Dec-11-2018

also, not related to your problem but still bug
print = (get_inverted_index(gettysburg_address))
should be
print(get_inverted_index(gettysburg_address))