0

I would like to read the credentials from the external file into a loop, so every time the password is incorrect it will ask me once again.

def register():
    username = input("Please input the first 2 letters of your first name and your birth year ")
    password = input("Please input your desired password ")
    file = open("accountfile.txt","a")
    file.write(username)
    file.write(" ")
    file.write(password)
    file.write("\n")
    file.close()
    if login():
        print("You are now logged in...")
    else:
        print("You aren't logged in!")

def login():
    username = input("Please enter your username")
    password = input("Please enter your password")  
    for line in open("accountfile.txt","r").readlines(): 
        login_info = line.split() # Split on the space, and store the results in a list of two strings
        if username == login_info[0] and password == login_info[1]:
            print("Correct credentials!")
            return True
    print("Incorrect credentials.")
    return False
David Silveiro
  • 1,515
  • 1
  • 15
  • 23
  • So add a `while not login(): pass` loop? – Aran-Fey Apr 07 '19 at 10:27
  • Possible duplicate of [Asking the user for input until they give a valid response](https://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) – tripleee Apr 07 '19 at 13:25

1 Answers1

0

Simply do this:

def register():
    username = input("Please input the first 2 letters of your first name and your birth year ")
    password = input("Please input your desired password ")
    with open("accountfile.txt","a") as file:
        file.write(username)
        file.write(" ")
        file.write(password)
        file.write("\n")
    log = 0
    while log != 1:
        if login():
            log = 1
            print("You are now logged in...")
        else:
            log = 0
            print("You aren't logged in! Please try again...\n")
Diogenis Siganos
  • 779
  • 7
  • 17