gpt4 book ai didi

python - 属性错误: 'list' object has no attribute 'title'

转载 作者:太空宇宙 更新时间:2023-11-03 21:03:51 24 4
gpt4 key购买 nike

enter code here我正在阅读《Python 速成类(class)》,我列了一个简单的邀请参加晚宴的人员名单。我删除、添加和替换了列表上的姓名,问题是:当我尝试打印更新/新的人员列表时,我收到此错误,我尝试了我能想到的一切:

AttributeError: 'list' object has no attribute 'title'

我更改了变量名称“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

最佳答案

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

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

关于python - 属性错误: 'list' object has no attribute 'title' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55555016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com