KEYWORD_CATEGORIES = {\'happy\': [\'great\',\'excited\',\'happy\',\'awesome\'],
ID: 3680928 • Letter: K
Question
KEYWORD_CATEGORIES = {'happy': ['great','excited','happy','awesome'], 'uncertain': ['maybe','worried','dunno'], 'sad': ['unhappy','miserable','awful'], 'good': ['pleasant','lovely','nice']}
This is what I've done:
def keyword_category(keyword):
keyword = keyword.lower()
for key in KEYWORD_CATEGORIES:
for value in KEYWORD_CATEGORIES[key]:
if keyword in value:
return KEYWORD_CATEGORIES[key]
return "undefined"
Output:
>>> keyword_category('awful')
['unhappy', 'miserable', 'awful']
Can you help me to fix this so that it'll return like this: keyword_category("great") and keyword_category("AWESOME") would both return "happy", keyword_category("Maybe") would return "uncertain", and keyword_category("the") might return "undefined" (assuming that "the" doesn't turn up as a value for any of the categories.
Explanation / Answer
keyword.py
def keyword_category(keyword):
KEYWORD_CATEGORIES = {'happy': ['great','excited','happy','awesome'], 'uncertain': ['maybe','worried','dunno'], 'sad': ['unhappy','miserable','awful'], 'good': ['pleasant','lovely','nice']}
keyword = keyword.lower()
for key in KEYWORD_CATEGORIES:
if keyword in KEYWORD_CATEGORIES[key]:
return key
return "undefined"
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.