我在尝试追加字典时遇到了一些麻烦,我真的不知道如何使用它,因为每次我尝试运行我的代码时,它都会说“'str' 对象没有属性 'append'”...我有这样的东西......
oscars= {
'Best movie': ['The shape of water','Lady Bird','Dunkirk'],
'Best actress':['Meryl Streep','Frances McDormand'],
'Best actor': ['Gary Oldman','Denzel Washington']
}
所以我想做一个新的类别,然后我想做一个循环,用户可以输入他想要的尽可能多的被提名者......
newcategory=input("Enter new category: ")
nominees=input("Enter a nominee: ")
oscars[newcategory]=nominees
addnewnominees= str(input("Do you want to enter more nominees: (yes/no):"))
while addnewnominees!= "No":
nominees=input("Enter new nominee: ")
oscars[newcategory].append(nominees)
addnewnominees= str(input("Do you want to enter more nominees: (yes/no):"))
有人知道如何在字典中使用追加吗?
请您参考如下方法:
您不能 append 到字符串。首先形成一个列表,以便稍后可以 append 到它:
oscars[newcategory] = [nominees]
