1

I'm writing a program which will need a user to register and login with an account. I get the program to have the user make their username and password which are saved in an external text file (accountfile.txt), but when it comes to the login I have no idea how to get the program to check if what the user inputs is present in the text file.

This is what my bit of code looks like :

def main():
    register()

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.close()
    login()

def login():
    check = open("accountfile.txt","r")
    username = input("Please enter your username")
    password = input("Please enter your password")

I have no idea what to do from this point.

Also, this is what a registered account would look like in the text file:

Ha2001 examplepassword
Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
OSG
  • 57
  • 1
  • 2
  • 10
  • It seems a near duplicate question of [this question](https://stackoverflow.com/questions/1904394/read-only-the-first-line-of-a-file). Once you have the line use split to separate the two fields. I suggest to use a tab as separator instead of space. – Roberto Trani Oct 13 '17 at 22:49
  • You need to take some decisions about what login function should do. Maybe you would like to return `True` or `False` if login is successfull r whatever... you should know it. –  Oct 13 '17 at 22:50
  • You need to read file **check**, split each line into its separate words, and compare those to the entered user name and password. Continue line by line until you get to the end of file or find the matching username. – Prune Oct 13 '17 at 22:50
  • Why do you need to store passwords? – Peter Wood Oct 13 '17 at 22:53
  • 2
    I hope this is for a school assignment, and not for a “real” user account system. Storing passwords in a plaintext flat file is a *horrible* idea. – dan04 Oct 13 '17 at 23:04

1 Answers1

2

After opening the file, you can use readlines() to read the text into a list of username/password pairs. Since you separated username and password with a space, each pair is string that looks like 'Na19XX myPassword', which you can split into a list of two strings with split(). From there, check whether the username and password match the user input. If you want multiple users as your TXT file grows, you need to add a newline after each username/password pair.

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(): # Read the lines
        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
Brenden Petersen
  • 1,993
  • 1
  • 9
  • 10
  • Your solution seems to work, but only for the very first account added in the text file, I need to make this work for more than one account. – OSG Oct 13 '17 at 23:22
  • Ah, if that's the case, then you will have to insert a newline character (`'\n'`) after each username/password combination. I edited my answer above, which should now work as the txt file grows. – Brenden Petersen Oct 13 '17 at 23:38