Hermi Bv
Hermi Bv

Reputation: 53

Checking if the content of a dictionary is True

I have a dictionary(names) with other dictionarys inside. My task is to look for an specific key and if the content that key is True.

hermes = {"Español":True, "Ingles":True, "Chino":False, "Frances":False, "Italiano":True}
jose = {"Español":True, "Ingles":False, "Chino":False, "Frances":False, "Italiano":True}
lupe = {"Español":True, "Ingles":True, "Chino":True, "Frances":False, "Italiano":True}
nacho = {"Español":False, "Ingles":True, "Chino":False, "Frances":True, "Italiano":False}
luis = {"Español":False, "Ingles":False, "Chino":True, "Frances":True, "Italiano":False}
names = {"Hermes":hermes, "José":jose, "Lupe":lupe, "Nacho":nacho, "Luis":luis}
x = input("Ingrese el idioma requerido: ")
for postu in names:
  for k,c in names[key].items():
    if k == x and c==True:
      print(postu)

Whenever i run the program nothing happens after it asks me the input. I've tried to just print k and c to see if the key and contents dont appear but it works perfectly. My problem happens trying to check if c is True.

Upvotes: 1

Views: 68

Answers (1)

Samwise
Samwise

Reputation: 71454

You don't define key anywhere, so I'm not sure what that's trying to do. If you're trying to print all the people who speak the inputted language, that would be more like:

x = input("Ingrese el idioma requerido: ")
for name, speaks in names.items():
    if speaks[x]:
        print(name)

Or you could get the names as a list by doing:

x_speakers = [name for name, speaks in names.items() if speaks[x]]

Upvotes: 4

Related Questions