-1

Hello I am a student and I have been researching on here for way too long to find answers to how to make this login page actually work. All the pages I have found have made them in different ways that don't work for me. Can anyone off this code push me on the right track to create this. I have got my signup page to work though.

def signup():
    
    users = open("login_signup.txt","a")
    user = []
    username = input("What do you want your username to be?: ")
    password = input("What do you want your password to be?: ")
    user.append(username)
    user.append(password)
    users.write(username + "," + password)
    users.write("\n")

    print("You have successfully signed up")


def login():
    
    with open("login_signup.txt","r") as users:
        
        usersusername = input("What is your username?: ")
        userspassword = input("What is your password?: ")

Btw the format off the text in the file is: username,password

Then it goes to a new line after the next person wants to create an account.

Thanks to anyone who can help :)

zwer
  • 24,943
  • 3
  • 48
  • 66
RmT
  • 13
  • 3
  • You have opened the file so far. Read a line in that file. Split that line by a comma. Index and store [0] as the username and [1] as the password. Stored usr & pwd to the newly inputed usr & pwd. – Buddy Bob May 18 '21 at 03:43
  • I'm getting lost up to the index and store bit how do you do that? – RmT May 18 '21 at 03:49
  • If you have text such as `"a,b"` and preform `split(',')` on this you will have a list. `['a','b']`. In your case if you index this new list as `text[0]` you will get the username. Then if you index this list as `text[1]` you will get the password. – Buddy Bob May 18 '21 at 03:51
  • Ok that makes more sense I will try and see what I can do. Thank you for your time – RmT May 18 '21 at 03:53
  • Does this answer your question? [How to check text file for usernames and passwords](https://stackoverflow.com/questions/46738966/how-to-check-text-file-for-usernames-and-passwords) – DarrylG May 18 '21 at 03:53
  • You don't even need to index anything, just read the file line by line and check if the line matches what user entered, e.g. `for line in users: if line.strip() == f'{usersusername},{userspassword}': ...` – zwer May 18 '21 at 03:53
  • def login(): # Should read text file and check if the password and username are correct with open("login_signup.txt","r") as users: usersusername = input("What is your username?: ") userspassword = input("What is your password?: ") for line in users: if line.strip() == f'{usersusername},{userspassword}': print("You have successfully signed in!") else: print("Your credentials were incorrect please try again!") login() Is this good? – RmT May 18 '21 at 04:01
  • 1
    I know this isn’t the main thrust of your question, but this is a very bad way to manage passwords. There is no reason to ever store a password in plaintext, and there are many libraries and services you can use that have secure and flexible user authentication. – Chris Johnson May 18 '21 at 04:51
  • @ChrisJohnson its just for a school project thing and it is allowed to be in a text file and for other aspects in my project I need to use this logic. And the code above doesn't work. – RmT May 18 '21 at 05:35
  • I just saw your comment now thanks @DarrylG I am currently looking at it now – RmT May 18 '21 at 10:04
  • In the linked solution just change split from splitting on space (the default) to splitting on comma i.e. `login_info = line.split(',')` – DarrylG May 18 '21 at 10:15
  • It still isn't working, I really don't know what is going on @DarrylG – RmT May 18 '21 at 11:51
  • @RmT--does my answer work for you? Is it understandable? – DarrylG May 18 '21 at 13:07
  • @DarryIG I can’t get it to work for some reason – RmT May 18 '21 at 22:13

1 Answers1

1

Since you're still having problems here's a modification of your code that works.

def signup():
    
    with open("login_signup.txt","a") as users: # context manager is preferred over pure open/close
        #user = []                              # not used so remove
        username = input("What do you want your username to be?: ")
        password = input("What do you want your password to be?: ")
        #user.append(username)                  # not used so remove
        #user.append(password)                  # not used so remove
        users.write(username + "," + password)
        users.write("\n")

        print("You have successfully signed up")


def login():
    
    usersname = input("What is your username?: ")
    userspassword = input("What is your password?: ")
  
    with open("login_signup.txt", "r") as users:

        for line in users:                              # iterating over login file a line at a time
            login_info = line.rstrip().split(',')       # rstrip() removes the '\n' at end of string
                                                        # split(',' split string on comma 
            if usersname == login_info[0] and userspassword == login_info[1]:
                print("Correct credentials!")
                return True
        
    print("Incorrect credentials.")
    return False

Exmaple Run

sigup()
# Out:
#     What do you want your username to be?: john
#     What do you want your password to be?: paul
#     You have successfully signed up


login()
# Out:
#    What is your username?: john
#    What is your password?: paul
#    Correct credentials!
#    True
DarrylG
  • 16,732
  • 2
  • 17
  • 23