Python giving list out of bounds but it shows both elements in the list

Question:

I’m trying to make a simple log in program using python (still fairly new to it), and I have the log in information stored in a text file for the user to match in order to successfully log in. Whenever I run it it says “list index out of range” but I’m able to print out the value of that element in the list, which is where I’m confused. I am trying to add the first line (username) and the second line (password) in the file to the list to compare to the user inputted values for each field, but am unable to compare them.

def main():
    username = getUsername()
    password = getPassword()
    authenticateUser(username, password)


def getUsername():
    username = input("Please enter your username: ")

    return username


def getPassword():
    password = input("Please enter your password: ")

    return password


def authenticateUser(username, password):
    credentials = []
    with open("account_information.txt") as f:
        content = f.read()
        credentials.append(content)

    if(username == credentials[0] and password == credentials[1]):
        print("Login Successful!")

    else:
        print("Login Failed")



main()
Asked By: Alex S

||

Answers:

You should use readline to get your infos in a list :

def authenticateUser(username, password):
    credentials = []
    with open("account_information.txt") as f:
        content = f.readlines()

Using this, accordingly to what you describe, content[0] will be your username and content[1] your password.

Answered By: iFlo

If you look at the python documentation at 7.2.1. Methods of File Objects, you’ll see

To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string or bytes object

You will see that f.read() is not the function you are looking for. try f.readlines() instead to put the content in a list of lines.

If you want to read all the lines of a file in a list you can also use list(f) or f.readlines().

Answered By: Stijn Diependaele

Depending on what is in your account_information.txt, file.read might not be the function you want to use.

Indeed, read will return a string, that is a list of all the characters in the file. So if you have your username and password on two separate lines for instance

foo
H0weSomeP4ssworD

you may instead use readlines to parse your file into a list of strings where each element is a line within the file.

Answered By: Buddyshot

After having "your problem solved", I invite you to come back to your problem description:

Python giving list out of bounds but it shows both elements in the list

What have you learned besides the fact that you have used the wrong method? I hope also something about your diagnostic skills. How should you print out a list to see how many items it contains?

If you do it like this

items = []
items.append("hellonworld")
for i in items:
    print(i)

you’ll see:

hello
world

If you therefore deduce to have 2 items in the list, is this correct?

And python definitely reports that you’re accessing the list out of bounds. You saw the conflict between your and python’s perspective.

I think you should at least have learned using len() for out-of-bounds diagnostics today.

Answered By: Wolf