naraemee naraemee - 10 days ago 4
Python Question

Sorting random element into dictionary? Python

I am trying to append a random choice into a dictionary, but my code doesn't seem to be working.

The file I am using (mood.txt):

happy, Jennifer Clause
happy, Jake Foster
sad, Jonathan Bower
mad, Penny
excited, Logan
awkward, Mason Tyme


my code:

def theFile():
moodFile = open("mood.txt")
theMood = moodFile.readlines()
moodFile.close()
return(theMood)

def makeTheDict(myFile):
moodDict = {}

for lines in myFile:
(mood, name) = lines.split(",")

moodDict[mood] = name.strip()

return(moodDict)


def randomMood(name, mood, moodDict):

if mood in moodDict:
randomMood = random.choice(mood)

moodDict[mood] = randomMood
moodDict.append(name, randomMood)

print(name, "has been put in the", randomMood, "group")


def main():

moodFile = theFile()

moodDict = makeTheDict(moodFile)


name = input("Choose a name: ")

newMood = input("Choose a mood: ")

randomMood(name, newMood, moodDict)


For example, I want to add a "Jamie Green" into a random group, and if it randomly chose "sad" then -

happy, Jennifer Clause
happy, Jake Foster
sad, Jonathan Bower
mad, Penny
excited, Logan
awkward, Mason Tyme
#sad, Jamie Green


How would I append this into the dictionary randomly?

Thank you!

Answer

It seems that you want to map strings to lists of strings, but instead of that you are mapping strings to strings.

Look at this line:

moodDict[mood] = name.strip()

Here you are mapping the string mood to the string name.strip(). If at this point, there was already a name mapped to the current mood, the old value would be replaced and lost. In your file sample, both Jennifer and Jake are happy. At the first iteration of the for loop you have:

moodDict["happy"] = "Jennifer Clause"

Then, at the second step, you have.

moodDict["happy"] = "Jake Foster"

Here "Jake Foster" replaces "Jennifer Clause". Since the moods can be repeated, what you probably want is something like this:

if mood in moodDict:
    moonDict[mood].append(name.strip())
else:
    moonDict[mood] = [name.strip()]

This way, for each mood key you have a list of name values.

Regarding the randomMood function, there are may things doesn't look good:

  • The if statement should be indented since is part of the function. This should throw an IndentationError, but I will assume it happened when you copied the code into StackOverflow.
  • Mood is a string, so what you are actually doing in random.choice(mood) is to choose a random character from that string, which doesn't make any sense. You probably want to choose from the list of moods, which would be something like this randomMood = random.choice(moodDict.keys()).
  • Because of what I explained in the previous point, the following line just replace the value under the mood key with a random character from the mood, which doesn't make sense.
  • Dictionaries don't have any method named append, this should throw an error too. You probably want to replace it with this: moonDict[randomMood].append(name)
  • Finally, I don't understand why you ask the user to input a mood when it is supposed to be chosen randomly.

It seems you are a bit confused about what a Python dictionary is and how it works. Remember that it map keys to values. In your code your keys are the moods and the values are the names, both represented as strings. The keys are unique. This means that if you assign a new value to an existing key, the old value mapped under that key gets lost. If you want to deal with multiple values under the same key you should map the key to a collection of values, like a list, instead of a single value.

Comments