AttributeError:“列表”对象没有属性“标题”

时间:2019-04-07 02:06:46

标签: python

enter code here我正在阅读“ Python速成课程”,并列出了一个简单的邀请参加晚餐的人。我在列表上删除,添加和替换了一个名称,问题是:当我尝试打印更新的/新的人员列表时,出现此错误,我尝试了我能想到的一切:

  

AttributeError:“列表”对象没有属性“标题”

我更改了变量名“ Dinner”,再次检查了变量的每次使用。我不知道是什么错误。

___________________________________________

Dinner = ["Emiel", "Louie", "Ben", "Jim", "Ant"]

Invite_Emiel = "Hello" + " " + Dinner[0] + " " + "Would you like to go to dinner with me and some others?\n"
print(Invite_Emiel)


Invite_Louie = "Hello" + " " + Dinner[1] + " " + "Would you like to go to dinner with me and some others?\n"
print(Invite_Louie)


Invite_Ben = "Hello" + " " + Dinner[2] + " " + "Would you like to go to dinner with me and some others?\n"
print(Invite_Ben)


Invite_Jim = "Hello" + " " + Dinner[3] + " " + "Would you like to go to dinner with me and some others?\n"
print(Invite_Jim)


Invite_Ant = "Hello" + " " + Dinner[4] + " " + "Would you like to go to dinner with me and some others?\n"
print(Invite_Ant)


cant_go = Dinner[1] + " " + "Can't go to the party, he called, his daughter died.\n"
print(cant_go)


Dinner_remove = Dinner.remove('Louie')


Dinner_add = Dinner.insert(1, 'Chris')


New_People = "Here's the list for the people coming to dinner:" + " " + Dinner.title()
print(Dinner)

完全错误:

Traceback (most recent call last):
File "C:\Users\Tyler\Desktop\PCC TS\pcc.py", line 28, in <module>
New_People = "Here's the list for the people coming to dinner:" + " " + Dinner.title()
AttributeError: 'list' object has no attribute 'title'

就像我说的那样,我对出什么问题或如何解决没有任何想法。

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:0)

问题就出在这里,即使我看不到Dinner是如何定义的。 Dinner是列表对象,列表对象没有title方法,因此Dinner.title()是无效的构造。

您对Dinner的所有其他使用都与它的列表一致。致电Dinner.title()时,您真正想要返回什么?似乎您想要返回Dinner中的项目列表,以便可以为用户打印出来。

您可以将它们与join ... " ".join(Dinner)一起添加。

此外,我假设您的最后一行应为print(New_People)

答案 1 :(得分:0)

似乎Dinner变量是列表/元组类型,没有错误消息显示的属性title()。我假设您要将列表打印成字符串。尝试更改线

New_People = "Here's the list for the people coming to dinner:" + " " + Dinner.title()

New_People = "Here's the list for the people coming to dinner:" + " " + ",".join(Dinner)

答案 2 :(得分:0)

实际上,列表没有.title()函数,您可以执行以下操作:

New_People = "Here's the list for the people coming to dinner:" + " " + str(Dinner)
print(New_People)