Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python List object attribute 'append' is read-only

Tags:

python

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
like image 380
Kraegan Epsilon Avatar asked Oct 26 '15 21:10

Kraegan Epsilon


People also ask

What is append in Python with example?

append() will place new items in the available space. Lists are sequences that can hold different data types and Python objects, so you can use . append() to add any object to a given list. In this example, you first add an integer number, then a string, and finally a floating-point number.

Has no attribute append Python?

The Python "AttributeError: 'NoneType' object has no attribute 'append'" occurs when we try to call the append() method on a None value, e.g. assignment from a function that doesn't return anything. To solve the error, make sure to only call append() on list objects.


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).

like image 149
mgilson Avatar answered Sep 19 '22 21:09

mgilson


or you could modify the same list slot by doing:

passwords[passwords.index('mrjoeadmin')] = Choice13
like image 42
eyalsn Avatar answered Sep 18 '22 21:09

eyalsn