So I made the login program that reads the usernames and password from a file (names.txt).
The program works correctly. But my problem is I want to ask give the user 3 chances to get the username right before the program closes and 10 chances to get the password right before the program closes.
Using loops isn't working. What should I be doing differently?

Here's my program

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']) and (code in A['password']):
        print("Successful Login")

    else:
        print("Username or Password Incorrect") 


main()

I was wrong. for loop works.

for i in range(3):   
        name=input("Username: ", )
        if (name in A['user']):
            break
        else:

            print("Username incorrect")



    for i in range(10):
        code=input("Password: ", )
        if (code in A['password']):
            break
        else:

            print("incorrect password")

    if (name in A['user']) and (code in A['password']):
        print("Successful Login")

But when the user get, for example, the username wrong more than 3 times I want to close the program but instead my program just starts asking for the password!

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