这是我的字典,其中的值反过来也是字典本身。
rarebirds = {
'Golden-crested Toucan' : {
'Height (m)': 1.1,
'Weight (kg)': 35,
'Color':'Gold',
'Endangered': True,
'Agressive': True
},
'Pearlescent Kingfisher': {
'Height (m)': .25,
'Weight (kg)': .5,
'Color':'White',
'Endangered': False,
'Agressive': False
},
'Four-metre Hummingbird': {
'Height (m)': .6,
'Weight (kg)': .5,
'Color':'Blue',
'Endangered': True,
'Agressive': False
},
'Giant Eagle': {
'Height (m)': 1.5,
'Weight (kg)': 52,
'Color':'Black and White',
'Endangered': True,
'Agressive': True
},
'Ancient Vulture': {
'Height (m)': 2.1,
'Weight (kg)': 70,
'Color':'Brown',
'Endangered': False,
'Agressive': False
}
}下面是不起作用的for循环。
actions = ['Back Away',
'Cover our Heads'
'Take a Photograph']
for i in rarebirds:
if (i,'Aggressive')==True:
print(i+": "+(actions[1]))
else: print(i+ " is not aggressive")它的输出
Golden-crested Toucan is not aggressive
Pearlescent Kingfisher is not aggressive
Four-metre Hummingbird is not aggressive
Giant Eagle is not aggressive
Ancient Vulture is not aggressive但是根据我的字典,巨嘴鸟和鹰是好斗的,所以我想让for循环打印我的标题为'actions‘的列表的index1。不确定循环无法成功识别True布尔值的原因。
我哪里错了?任何帮助都将不胜感激。
发布于 2020-04-29 02:11:35
已解决
for i in rarebirds:
if rarebirds[i]['Endangered']==True:
print(i+": "+ (actions[1]))
else: print(i+ " is not aggressive")https://stackoverflow.com/questions/61485515
复制相似问题