I have to make a login program where the username and password are taken from an existing file.
I have successfully, in my opinion, opened, read, and brought out all the user names and passwords from the file.
The problem is, the user is only allowed to input an incorrect username 3 times before the program end.
Also, the user is given a total of 10 tries all together. (ex. user puts 2 wrong usernames before putting in the right one, then they have 8 tries to input a correct password or, again, the program ends.)

Here's what I have

def main():
    file = open("names.txt", 'r')
    line = file.readline()
    A={}


    s=line.split()
    user=s[3]
    password=s[4]


    name=input("Username: ", )
    code=input("Password: ", )

    for i in range(3):
        if name in user:
            print("welcome", name)
        else:
            print("Incorrect username")



main()

I'm not sure how to put it so that it the input of the username is given 3 attempts and that the password is given 10(if username correct), 9(if one incorrect username), and 8(if two incorrect username) tries.

Edited 3 Years Ago by dp121307

I do not see you updating the incorrect tries count, and you are doing same test three times in lines 15-19. A is defined but not used, and it does not look like class name even it is Capitalized. Also the code variable is not used. Are you sure that the file has only one line?

My apologies, I am taking my first course in programming and the reason why I started the thread is because I don't know exactly what to do.
I was working with dictionaries which is why A is there but I removed it.
For the code variable I would do the same thing I would for name (I think?) but I still haven't gotten name right.
The file has 99 lines but when I read lineS I get AttributeError: 'list' object has no attribute 'split'. It's worked for read line, however.

I've been working on the code (will waiting for any suggestions)! Here's what I have so far:

def main():

    file = open("names.txt", 'r')
    line = file.readline()
    A={}
    while line:

        s=line.split()
        user=s[3]
        password=s[4]



        A['user']=s[3]
        A['password']=s[4]

        line = file.readline()
        print(A)

    name=input("Username: ", )
    code=input("Password: ", )

    if name in A['user']:
        if code in A['password']:
            print("Successful Login")
    else:
            print("Username or Password Incorrect")






main()

When the username and password are correct I get the successful login message.
Now, when the username or password are incorrect I get no print statements? Anyone can help me figure out what I'm doing wrong?

The else statement only executes if the name is not correct, regardless of what the password is (name=correct & password=incorrect = nothing is printed). Note also that A['user'] and A[password'] will only contain the last value from the while loop, so you should print the dictionary to verify this.

Edited 3 Years Ago by woooee

I did print the dictionary, and the program reads all of the usernames and passwords.
If I use elif for A['password'], I still get the same results. I'm so stuck with this one! I don't know what to do anymore. I've been working on it piece by piece to see what I can do differently but everything, except the if/else statements, are working as they should.

Combine the two if statements

    if (name in A['user']) and (code in A['password']):
            print("Successful Login")
    else:
            print("Username or Password Incorrect")

Thank you! I was trying to do that before but I was doing if name, code in A. Obviously I was very very wrong.

Notice that you do not want to use comparisons like

'u' in 'username' and 'a' in 'nice_password'

for input u for userrame and a for password. You must have exact match.

Also password must be the one belonging to certain user, not one password among all the passwords of all users. I still do not understand the purpose of A dictionary.

Edited 3 Years Ago by pyTony

As a hint, the dictionary can only contain data from the file. Strings like"user" or "password" can not be used as keys or anywhere in the dictionary. Nor can anything else that is not from the file.

Edited 3 Years Ago by woooee

This article has been dead for over six months. Start a new discussion instead.