0

So I'm making a program where I need a user to log in or register. The registered account goes to a .txt file from which I'm supposed to read the data to log in again.

I managed to get the basics working. I can register a new account to the file and I can log in with every account I've created, but I can't seem to get 2 important elements working. The first one is for when the user inserts an inexistent username/ password (in this case the program just does nothing as I can't figure out a condition to make it go back to asking the username and password), and the second one is for when I insert a username and password that don't match. Here the program goes back and asks for them again but then keeps asking, even if I put them correctly.

Here's my function if anyone's interested in having a look at it:

def ent():
    util = False
    ppass = False
    login = False
    while not login:
        n_util = input("Introduce your username: ")
        password = input("Introduce your password: ")
        with open("dadoscontas.txt", "r") as f:
            while not util:
                vski = 0
                for line in f:
                    vski += 1
                    if vski == 1:
                        if line.strip() == n_util:
                            util = True
                        else:
                            break
                    if vski == 2:
                        if line.strip() == password and user:
                            ppass = True
                        if user and ppass:
                            login = True
    print("Logged in")

I've spent my whole afternoon trying different things to see if I can get these 2 things to work, but I can't. As I said, the function above is the part that kinda works, and if anyone could give any suggestions / point me in the right direction it would be really helpful. Thank you in advance.

emanuxd11
  • 11
  • 1
  • 2
    https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response – Prune Mar 16 '21 at 23:00

1 Answers1

0

Does this code cover your needs?

def ent():
    util = False
    login = False
    while not login:
        n_util = input("Introduce your username: ")
        password = input("Introduce your password: ")
        with open("some_test.txt", "r") as f:
            vski = 0
            for line in f:
                vski += 1
                if vski%2:
                    if line.strip() == n_util:
                        util = True
                elif util:
                    if line.strip() == password:
                        login = True
                    else:
                        util = False
    print("Logged in")

Or you even could exit the function with return in if line.strip() == password: block. But i would recommend you to store the file content to dictionaries (user_name:passwor), because you are parsing the whole file again and again while login=False:

def ent():
    login = False
    name=""
    my_data = {}
    with open("some_test.txt", "r") as f:
        index = 0
        for line in f:
            index += 1
            if index%2:
                name = line.strip()
            else:
                my_data[name] = line.strip()
    while not login:
        n_util = input("Introduce your username: ")
        password = input("Introduce your password: ")
        if n_util in my_data and my_data[n_util] == password:
            login = True
    print("Logged in")

If you use python2 you can use .get() or try instead of n_util in my_data for better performance.

Avo Asatryan
  • 404
  • 8
  • 21