x = input("Color: ")
list = [
{"Color": "Red", "Mood": "Angry"},
{"Color": "Blue", "Mood": "Sad"},
{"Color": "Green", "Mood": "Jealous"}
]
for mood in list: #???
print (mood["Mood"]) #???`
If I prompt the user for a color, I want it to ONLY print the "mood" associated with that color in the dictionary. The for loop above is what I can come up with, but that will print all moods "Angry", "Sad", "Jealous", no matter which color the user inputs.
I am just learning and am stuck on where to go from here using the "for" loop. (I know I could make several boolean expressions for each color/mood combo but I am practicing with using dictionaries so this is the preferred method)"
CodePudding user response:
You should really change the way you organize the data. Compute a dictionary with colors as keys, moods as items:
l = [
{"Color": "Red", "Mood": "Angry"},
{"Color": "Blue", "Mood": "Sad"},
{"Color": "Green", "Mood": "Jealous"}
]
moods = {d['Color']: d['Mood'] for d in l}
# {'Red': 'Angry', 'Blue': 'Sad', 'Green': 'Jealous'}
Then, no need for a loop:
print(moods.get(input('Color: '), 'unknown color!'))
CodePudding user response:
Okay, let's look at what your code does
x = input("Color: ")
This asks the user for a color and assigns it to the variable "x".
list = [
{"Color": "Red", "Mood": "Angry"},
{"Color": "Blue", "Mood": "Sad"},
{"Color": "Green", "Mood": "Jealous"}
]
This makes a list that contains three separate dictionaries - one has the "Color" "Red" and "Mood" "Angry", the second has the "Color" "Blue" and the "Mood" "Sad" and so on.
for mood in list: #???
This goes over all entries in your list, so each run through the loop the loop variable "mood" will be one of the dictionaries above.
print (mood["Mood"]) #???`
This prints the current dictionary's "Mood" value.
So, your problem is that you don't ever check if the "Color" is x!
The simplest fix is probably making that check inside the for-loop:
for item in list: # it's not a "mood", we have the full dictionary
if item["Color"] == x:
print(item["Mood"])
# if we know the color will only be in the list once,
# we can break out of the loop
break
There are more concise ways of doing the same thing like list comprehensions which can do the filtering that for-loop does in a single line, something like
moods = [ it["Mood"] for it in list if it["Color"] == x ]
But those are more advanced topics.
It's also possible you just want to organize your data differently, for example you could just use the "Color" values as the dictionary keys and use a single dictionary:
colors_to_moods = {
"Red": "Angry",
"Blue": "Sad",
"Green": "Jealous"
}
print(colors_to_moods[x])
Which of these you want to use depends on what you want to do with your program otherwise.
CodePudding user response:
If you want to stick with your current list you can test for x
:
x = input("Color: ")
color_to_mood = [
{"Color": "Red", "Mood": "Angry"},
{"Color": "Blue", "Mood": "Sad"},
{"Color": "Green", "Mood": "Jealous"}
]
for c2m in color_to_mood:
if x == c2m['Color']:
print (c2m["Mood"])
CodePudding user response:
It is better to use a dictionary like this:
x = input('color: ')
colors = {
'red' : 'angry',
'green' : 'jealous',
'blue' : 'sad'
}
print ('your mood is: ',colors[x])