所以我想調用一個字典中的字典並使用這些鍵作爲值,但是我所有嘗試達到它的失敗迄今爲止。這是我的位置,請注意,if表達式之後的所有打印語句都會失敗。字典中的字典調用
def main():
print("This program tells you about the animal of your choice")
animal=input("What animal would you like to look up: ")
animal=animal.lower()
d2={
"lion":{"name":"Lion","species":"Panthera leo",
"image":"http://en.wikipedia.org/wiki/File:Lion_waiting_in_Namibia.jpg",
"fact":"Vulnerable species"},
"dog":{"name:":"Dog","species":"Canis lupus familiaris",
"image":"http://en.wikipedia.org/wiki/File:YellowLabradorLooking_new.jpg",
"fact":"Common house pet"},
"hippo":{"name":"Hippo","species":"Hippopotamus amphibius",
"image":"https://en.wikipedia.org/wiki/Hippopotamus#/media/File:Hippopotamus_-_04.jpg",
"fact":"Erbivorous mammal"},
"cat":{"name":"Cat","species":"Felis catus",
"image":"https://en.wikipedia.org/wiki/Cat#/media/File:Cat_poster_1.jpg",
"fact":"Purring hunters"}
}
if animal in d2:
print(d2(animal["name"]), "is the common name")
print(d2(animal["species"]), "is its latin name")
print(d2(animal["image"]), "is a picture of", animal)
print(d2(animal["fact"]),)
else:
print("Not in dicionary, try lion, dog, hippo, or cat")
main()
當我粘貼代碼時,某些縮進消失,所以這就是爲什麼文本看起來如此混亂在d2 – Magnus
'dict'元素由'[]'而不是'()'訪問。 – sobolevn
您是否注意到您使用圓括號代替方括號來訪問字典? – TigerhawkT3