0

I'm not sure if this is a duplicate as I have looked around but nothing really answered my problem. I have this school homework that I have to do over the summer and the first task is making a system the allows the user to make an account which saves to a .txt and they are then able to log in using the account they made. I am having a problem making the logging in part, even if I input the correct username and password it will not log me in.

if neworlog == 2: #Option 2, log in
    logind = open("UserLoginDetails.txt","r")
    username = logind.readlines()[0]
    logind.seek(0)
    password = logind.readlines()[1]
    inputusnm = input("Please enter your username:\n")
    inputpswd = input("Please enter your password:\n")
    if inputusnm == username and inputpswd == password:
        print("You are now logged in")
        loggedin = True
    else:
        print("The entered username or password are incorrect")
        print("Please try again")

That is my code (might be cringe worthy, idk I'm learning) for logging in. The 'logind' variable is for the .txt, the 'username' and 'password' are the variables for the correct username and password from the text file and the 'inputusnm' and 'inputpswd' variable are for the inputed username and password.

I have tried trouble shooting it. I have tried using just a normal string for the 'username' and 'password' variable and that logged me in and I also tried printing out the 'username' and 'password' variable when they were reading the .txt and it printed out the correct lines/word.

This is a picture of the .txt file:

picture

I am kinda lost at this point and any help would be appreciated.

5
  • Did you have a question / problem? Commented Aug 6, 2017 at 12:00
  • 1
    If you try print(repr(username)) and print(repr(password)) you'll see that end with a newline character \n. You can use username = username.rstrip('\n') to remove it. Commented Aug 6, 2017 at 12:11
  • When you read in the username or password, they contain the new line character - '\n' at the end. You need to get rid of it. You can use .strip() on these strings to remove the extra spaces. Also your question isn't clear at all. The above suggestion is based on a guess that you aren't able to log in. Please edit the question and specifically ask what you need? Commented Aug 6, 2017 at 12:13
  • If you can use it for your homework, this could be a perfect use case of the ConfigParser module. Also, @Rawing, @ClockSlave are right. Commented Aug 6, 2017 at 12:17
  • Thanks for pointing out that I didn't really put down my problem, I have now. Commented Aug 6, 2017 at 12:22

1 Answer 1

1

May be it help you: simple get next line, so you don't need to do seek, and strip for clear not visible chars

username = next(logind).strip()
password = next(logind).strip()
Sign up to request clarification or add additional context in comments.

3 Comments

Careful with strip - this strips all whitespace, not just newlines. Users whose username or password starts/ends with whitespace won't be able to log in.
Thanks, this worked but I will keep in mind the problem with strip
@FLAiR glad to help you

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.