Python Forum

Full Version: Dictionary Homework
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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))
gettysburg_address is a str, i.e. "Four score and.."
you need to pass dict as argument when call the function, not str
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.
also, not related to your problem but still bug
print = (get_inverted_index(gettysburg_address))
should be
print(get_inverted_index(gettysburg_address))