i'm a bit new and i'm working on a login mechanism. All the usernames and passwords are stored in a list like [[username,passwort],[username,password]], which is then written in a textfile in the format: Username Password
for example: Julia
x1234
Nick
doggoXX123
and so on
when the programm starts I want to put them back in a list of usernames and passwords. To do that I wrote a function called read_from_file(), which takes two lines from the text file (the username and underthat the password) in a list (called user_data) and than adds the list to the main list of users (called user_list) [username, password] -> (mainlist:) [[username],[password]] Here is the code:
def read_from_file():
user_list = []
f = open("userdata.txt", "r")
user_data = ["", ""]
line_counter = 0
for x in f:
user_data[line_counter] = x
line_counter += 1
print(line_counter)
if line_counter == 2:
print(user_list)
line_counter = 0
user_list.append(user_data)
Now heres my problem: When I append the user_data to the user_list, the second time I append it also the first list appended to the user_list changes for example:
user_list = [[Julia, x1234]]
-> second time for loop goes through
user_list = [[Nick, doggoXX123],[Nick, doggoXX123]]
and it should ACTUALLY be
user_list = [[Julia, x1234],[Nick, doggoXX123]]
eventough the first item in the list should be [Julia, x1234]
SO how to I stop it from changing?
Now it tested this with a var called i and put it into a list and then changed the var again and put it a second time in the list and it worked how I thought it should:
def test():
list = []
i = 1
list.append(i)
i += 1
list.append(i)
print(list)
print(i)
output was [1,2] ; 2
just as expected
Now why doesnt this work with the list? Why does the list change and the var not?