Python List Object Attribute 'Append' Is Read-Only

Python List Object Attribute 'Append' Is Read-Only

as the title says, in python, I'm trying to make it so when someone types in a choice (in this case Choice13) then it deletes the old password from the list passwords and adds the new one instead.

passwords = ['mrjoebblock' , 'mrjoefblock' , 'mrjoegblock', 'mrmjoeadmin' ]
if choice == '3':
    password = raw_input('Welcome admin! I\'m going to need your password ')
        if password == 'mrjoeadmin':
            print('Welcome Mr. Joe!')
            Choice11 = raw_input('What would you like to do? Press 1 for changing your admin password, 2 for viewing a class\'s comments, or 3 for changing a class\'s password')
            if Choice11 == '1':
                print('You have chosen to change your password! ')
                Choice12 = raw_input('You will need to put in your current password to access this feature ')
                if Choice12 == 'mrmajoeadmin':
                    Choice13 = raw_input('What would you like to change your password to? ')
                    passwords.remove('mrjoeadmin')
                    passwords.append = Choice13
2

2 Answers

To append something to a list, you need to call the append method:

passwords.append(Choice13)

As you've seen, assigning to the append method results in an exception as you shouldn't be replacing methods on builtin objects -- (If you want to modify a builtin type, the supported way to do that is via subclassing).

0

or you could modify the same list slot by doing:

passwords[passwords.index('mrjoeadmin')] = Choice13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Sarah Jenkins
Author

Sarah Jenkins

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.