I want my program to print only one random word from the list within my dictionary, but I can't seem to get the right syntax to do so. I attempted using popitem() to get a random value from the list, but it doesn't seem to be working. Here is my code:
import random
thesaurus = {
"happy":["glad", "blissful", "ecstatic", "at ease"],
"sad" :["bleak", "blue", "depressed"]
}
# input
phrase = input("Enter a phrase: ")
# turn input into list
part1 = phrase.split()
part2 = list(part1)
newlist = []
for x in part2:
s = thesaurus.get(x, x)
newlist.append(s)
print (newlist)
For example, if the input is
i am happy
The expected output would be
i am glad
or any random word from the list within the dictionary.
But, right now my output looks like this:
['i', 'am', ['glad', 'blissful', 'ecstatic', 'at ease']]
I know there is another thread involved with this, but it doesn't seem to address this specific issue.
Any help would be appreciated!
Aucun commentaire:
Enregistrer un commentaire