codeHunter
codeHunter

Reputation: 47

Why am I getting a AttributeError: 'str' object has no attribute 'remove' when attempting to remove an element using a variable or a string?

The purpose of this program is to create a list of names for people attending a party. I would like to be able to grant the user the ability to continue adding names until they chose YES as an option to exit the loop. However, I have am stomped when it comes to having them enter a name they would like to remove in case they added someone by accident or if they would like to edit the list and remove or replace someone.

I am currently a newbie to programming, hence the lack of classes to this code. Any help would be greatly appreciated. Thank you all in advance!

#Initialize empty list
    partyList = []

    #Initilize empty entry
    inviteeName = ''
    endEntry = ''

    #Run loop until YES is entered as a value
    while endEntry != "Yes":
        inviteeName = input("Please enter the name of the person you are inviting below." + "\nName: ")
        inviteeName = inviteeName.title()

        # Verifies if a name was not entered.
        while inviteeName == "":
            inviteeName = input("\nPlease enter the name of the person you are inviting below." + "\nName: ")
            inviteeName = inviteeName.title()
        endEntry = input("\tPress ENTER to continue or type Yes to finish: ")
        endEntry = endEntry.title()

        #Append every new name to the list
        partyList.append(inviteeName)

    #Adds the word "and" to finish sentence if there are more than one invitees. NOTE: Make a class please!
    numOfInvitees = len(partyList)
    if numOfInvitees > 1:
        partyList.insert(-1, 'and')

    #Remove brackets and quotes.
    partyList = ', '.join(partyList)

    #Print message
    print("\nThis will be your final message:\n" + str(partyList) + "\nYou are invited to my party!\n")

I was trying to use this to assist the user with removing names entered by accident.

    submit = input('Submit?: '.title())
    submit = submit.title()

    if submit == 'Yes':
        print('Invite has been sent!')
    elif submit == 'No':
        remNameConfirmation = input('Would you like to remove a name from the list?: ')
        remNameConfirmation = remNameConfirmation.title()
        if remNameConfirmation == 'Yes':
            uninviteName = (input('Who would you like to remove?: '))
            uninviteName = uninviteName.title()

Here is the line that is giving some trouble

            partyList.remove(uninviteName)     

    print(partyList)

Upvotes: 1

Views: 2314

Answers (1)

James
James

Reputation: 36691

When your code reaches

partyList = ', '.join(partyList)

it will set the variable partyList to a string. Since it is no longer a list it does not have the .remove method.

Upvotes: 2

Related Questions