猿问

字典中的Python列表

现在,我知道已经对该主题进行了一些介绍,但我听不懂。我将向您展示我的代码,希望这可以帮助您了解我的位置。


字典


father = {"Yin": ["yang","yaha"]}

此代码可以正常工作。


elif choice == "5":

    son = input("Enter the name of a son to get the name of his grandfather: ")

    if son in father:

        description = father[son]

        print("\n", son, "'s grandfather is", description[1])

    else:

        print("\nSorry, I don't know who that is", son)

这段代码没有,我只是希望它能够更改列表中的第二项(yaha)。


elif choice == "6":

    son = input("which grandfather and son pair need updating: ")

    if son in father:

        description = input("What's name of the grandfather?: ")

        son[father] = description[1]

        print("\n", son, "has been redefined.")

    else:

        print("\nThat person doesn't exist!  Try adding it.")

任何帮助,将不胜感激。


繁花如伊
浏览 134回答 2
2回答

开心每一天1111

我想你是说father[son][1] = description在第二个片段中。解释:father是dict,son是字符串,因此son[father]会引发AttributeError。description也是一个字符串,description[1]单个字符也是如此。father[son]另一方面,是一个列表,您可以将其元素分配给新字符串。

慕码人8056858

description = input("What's name of the grandfather?: ")son[father] = description因为描述是一个字符串
随时随地看视频慕课网APP

相关分类

Python
我要回答