简体   繁体   中英

Cant add 2 items to a list in brackets

I have a simple script. The problem is, when I add the username and password, it gives me an error. I want the code to add the username and password to a list in brackets. This is my code:

add_user = 1
password = 2
passwords = []
passwords.append[(add_user, password)]
print(passwords)

And the output should be something like this:

[(1, 2)]

The error: TypeError: 'builtin_function_or_method' object is not subscriptable

Option-1

passwords = list()
passwords.append((add_user, password))

Option-2:

passwords = list()
passwords += [(add_user, password)]
passwords.append[(add_user, password)]

should be:

passwords.append((add_user, password))

because append is a function.

append is a method, which is used for adding an item to a list , so it needs to be called with () . You can do the below changes to make your code working as required:

add_user = 1
password = 2
passwords = []
passwords.append((add_user, password)) #change is in this line
print(passwords)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM