现在,我知道已经对该主题进行了一些介绍,但我听不懂。我将向您展示我的代码,希望这可以帮助您了解我的位置。字典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.")任何帮助,将不胜感激。
2 回答

开心每一天1111
TA贡献1836条经验 获得超13个赞
我想你是说
father[son][1] = description
在第二个片段中。
解释:
father
是dict,son
是字符串,因此son[father]
会引发AttributeError
。description
也是一个字符串,description[1]
单个字符也是如此。father[son]
另一方面,是一个列表,您可以将其元素分配给新字符串。

慕码人8056858
TA贡献1803条经验 获得超6个赞
description = input("What's name of the grandfather?: ")
son[father] = description
因为描述是一个字符串
添加回答
举报
0/150
提交
取消