2

enter code hereI'm reading 'Python Crash Course', and i made a simple list of people to invite to dinner. I had remove, add and replace a name on the list, problem is: When i try to print the updated/new list of people, i get this Error, i tried everything i could think of:

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

I changed the variable name "Dinner", double-checked every use of Variables. I can't figure out what the error is.

___________________________________________

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)

Full-Error:

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'

Like i said, i don't have any ideas on what's wrong or how to fix it.

enter image description here

enter image description here

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
Retr0Sauce
  • 33
  • 1
  • 1
  • 4
  • 1
    Welcome to [SO]! The issue is pretty much what the error message says. `list` doesn't have a method named `title`. Edit your question and describe what your want to accomplish. That way others do not have to _guess_ in answering the question. – Sнаđошƒаӽ Apr 07 '19 at 02:15

3 Answers3

0

The problem is just what it says, even though I can't see how Dinner is defined. Dinner is a list object, and list objects don't have a title method, so Dinner.title() is an invalid construct.

All of your other uses of Dinner are consistent with it being a list. What do you really want returned when you call Dinner.title()? It seems like you want the list of items in Dinner returned so you can print them out for the user.

You could add them together with join... " ".join(Dinner).

Also, I assume your last line should be print(New_People)

CryptoFool
  • 21,719
  • 5
  • 26
  • 44
  • Ah! I see that @VietHTran had the same idea. He joins with commas where I did it with spaces, but our solutions are otherwise the same. – CryptoFool Apr 07 '19 at 02:19
  • When making the question, i accidentally left out the Dinner list, here it is: Dinner = ["Emiel", "Louie", "Ben", "Jim", "Ant"] – Retr0Sauce Apr 07 '19 at 17:23
0

It seems Dinner variable is a list/tuple type, which don't have the attribute title() as the error message showed. I assume you want to print the list into a string. Try changing the line

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

to

New_People = "Here's the list for the people coming to dinner:" + " " + ",".join(Dinner)
VietHTran
  • 2,233
  • 2
  • 9
  • 16
0

Actually there is no .title() function for lists, you can do the following:

New_People = "Here's the list for the people coming to dinner:" + " " + str(Dinner)
print(New_People)
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
Amany
  • 321
  • 1
  • 7
  • Please try not to add the same answer that has already been posted not once, but twice! – CryptoFool Apr 07 '19 at 02:21
  • @steve how is it the same answer as the existing ones? BTW, welcome to [SO], Amany! – Sнаđошƒаӽ Apr 07 '19 at 02:23
  • All the answers tell him what his problem is. He's expecting a list object to have a title() method. - what he does instead is not all that important as he doesn't ask how to do that. He just asks why he's getting that error, and two people already explained that to him. – CryptoFool Apr 07 '19 at 02:24
  • Another alternative as an aside would be better added as a comment on one of the other answers. The issue here isn't about points. It's about keeping S.O. a high quality resource. We'd rather not have three fundamentally identical answers to the same question. – CryptoFool Apr 07 '19 at 02:27
  • Description of the problem must be more or less the same in order for it to have any validity, obviously, because after all, the problem being described is same. But the solution this answer gives is different, which makes the answer different. – Sнаđошƒаӽ Apr 07 '19 at 02:27
  • But it's not different in terms answering the primary question the OP is asking – CryptoFool Apr 07 '19 at 02:28
  • @Steve, it is not duplication, it is normal to point out the problem and give him/her a solution. My solution wasn't the same to what was proposed. – Amany Apr 07 '19 at 02:28
  • @Sнаđошƒаӽ, thanks. – Amany Apr 07 '19 at 02:28
  • Yes, it was. Read the question. He's asking why he's getting the error. What was your answer to THAT question? It was the same answer given 5 minutes before you. - to quote the OP, "I can't figure out what the error is." - he wasn't asking about formatting alternatives. – CryptoFool Apr 07 '19 at 02:29
  • @steve you can argue the whole day, but it's anyone guess that this answer is fundamentally different from the other two. – Sнаđошƒаӽ Apr 07 '19 at 02:31
  • From a S.O. quality standpoint, it's clear to me that the first answer serves the next reader more easily than having to read through three answers instead of one to get the same fundamental information. That's the issue. Adding a third alternative made this Q/A less valuable, not more. - People finding this question aren't worried about if they should use `split` or call `str(var)` - rather, they are wondering why they're getting the error `AttributeError: 'list' object has no attribute ''` – CryptoFool Apr 07 '19 at 02:32