Check a subkey value using a random variable and a condition. KeyError: "x"
Question:
I’m having trouble with the dictionary code for checking the value of a subkey.
I want to get that:
- If
random_data
is a word that matches "gender": "male"
then I want to print "Word is Male"
- If
random_data
is a word that matches "gender": "female"
, then I want to print "Word is Female"
I think the problem is the row if random_data == data["gender"]["male"]
, so I get the error:
KeyError: "gender"
How to fix?
import random
data = {
#MALE
"escritorio": {
"gender": "male",
},
"viento": {
"gender": "male",
},
#FEMALE
"silla": {
"gender": "female",
},
"mesa": {
"gender": "female",
},
}
#random key: escritorio or viento or silla or mesa
random_data = random.choice(list(data))
if random_data == data["gender"]["male"]:
print(random_data, ": word is Male")
else:
print(random_data, ": word is Female")
Answers:
I think the problem is the row if random_data == data["gender"]["male"]
, so I get the error:
KeyError: "gender"
"gender"
is a nested key – i.e., it’s not in data.keys()
['escritorio', 'viento', 'silla', 'mesa']
, and "male"
is not a key anywhere at all [it’s a value paired to "gender"
]. What you want is probably something like:
if data[random_data]["gender"] == "male":
print(random_data, ": word is Male")
else:
print(random_data, ": word is Female")
or, using the ternary operator:
print(random_data, ": word is", "Male" if data[random_data]["gender"]=="male" else "Female")
You actually don’t even need conditions though – you can just have
print(random_data, ": word is", data[random_data]["gender"].capitalize())
The problem is that the value, "gender"
, is not a dictionary containing either "male"
or "female"
.
This may work:
if data[random_data]["gender"] == "male":
print(f"{random_data}: word is Male")
else:
print(f"{random_data}: word is Female")
I’m having trouble with the dictionary code for checking the value of a subkey.
I want to get that:
- If
random_data
is a word that matches"gender": "male"
then I want to print "Word is Male" - If
random_data
is a word that matches"gender": "female"
, then I want to print "Word is Female"
I think the problem is the row if random_data == data["gender"]["male"]
, so I get the error:
KeyError: "gender"
How to fix?
import random
data = {
#MALE
"escritorio": {
"gender": "male",
},
"viento": {
"gender": "male",
},
#FEMALE
"silla": {
"gender": "female",
},
"mesa": {
"gender": "female",
},
}
#random key: escritorio or viento or silla or mesa
random_data = random.choice(list(data))
if random_data == data["gender"]["male"]:
print(random_data, ": word is Male")
else:
print(random_data, ": word is Female")
I think the problem is the row
if random_data == data["gender"]["male"]
, so I get the error:KeyError: "gender"
"gender"
is a nested key – i.e., it’s not in data.keys()
['escritorio', 'viento', 'silla', 'mesa']
, and "male"
is not a key anywhere at all [it’s a value paired to "gender"
]. What you want is probably something like:
if data[random_data]["gender"] == "male":
print(random_data, ": word is Male")
else:
print(random_data, ": word is Female")
or, using the ternary operator:
print(random_data, ": word is", "Male" if data[random_data]["gender"]=="male" else "Female")
You actually don’t even need conditions though – you can just have
print(random_data, ": word is", data[random_data]["gender"].capitalize())
The problem is that the value, "gender"
, is not a dictionary containing either "male"
or "female"
.
This may work:
if data[random_data]["gender"] == "male":
print(f"{random_data}: word is Male")
else:
print(f"{random_data}: word is Female")