0

I want my login program to take a username and password input from the user, and then check it to see if it is the same to the usernames and passwords as in the files. However, the code isn't working and I think I'm missing something:

def login():
    with open("username.txt","r") as username:
        usrn_read = username.read()
        username.close()

    with open("password.txt","r") as password:
        passw_read = password.read()
        password.close()

    inp_usern = input("Username: ")
    inp_passw = input("Password: ")
    if inp_usern==usrn_read and inp_passw==passw_read:
        print("Succesful!")
        variable.open("database.txt","a")
        variable.write("Login succesful for: "+inp_usern)
        variable.write("Password: "+inp_passw)
        print("Your username and password is:")
        print(inp_usern)
        print(inp_passw)
        forward()
    else:
        print("Not valid input. Please try again.")
        inp_usern = []
        inp_passw = []
        login()

def end():
    print("Thankyou!")

def forward():
    print("This would continue to quiz!")

login()

Any help would be appreciated!


Username file

bob12
alexi90
john08

UPDATE

With the code below it works, but only on the first line of the text file:

def login():
username = open("username.txt","r")
usrn_read = username.readline().replace('\n', '')
username.close()

password = open("password.txt","r")
passw_read = password.readline().replace('\n', '')
username.close()

inp_usern = input("Username: ")
inp_passw = input("Password: ")
if inp_usern==usrn_read and inp_passw==passw_read:
    print("Succesful!")
    variable = open("database.txt","a")
    variable.write("\n Login succesful for: "+inp_usern)
    variable.write(" Password: "+inp_passw)
    print("Your username and password is:")
    print(inp_usern)
    print(inp_passw)
    forward()
else:
    print("Not valid input. Please try again.")
    inp_usern = []
    inp_passw = []
    login()

def forward():
    print("This would continue to quiz!")

login()

And here are the files (first is username, second is password, and they are separate files):

bob12
alexi90

bob00
alexi00

Now that it can read the first line separately from the other lines of the file, how do I get it to read the other lines separately as well? Thank you!

defpython
  • 1
  • 1
  • The username file has: bob12 (new line) alexi90 (new line) john08 And the passwords has: fxh Anything can be in the files, as I have a separate account creation program that does work, but I just need any values to work at the moment. – defpython Nov 18 '17 at 16:30
  • Is the question that you would to be able to login with any username/password pair? Because in order to read multiple lines from the file, you have to loop through it or use file.readlines(). See: https://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects – Kaiser Dandangi Nov 20 '17 at 02:22

4 Answers4

2

One small observation: if you are opening your file with context manager, you dont have to worry abt closing it.Thats the beauty of it

with open("file1.txt", "r") as filename
    Your actions

Get rid of those two file.close() statements

SethMMorton
  • 45,752
  • 12
  • 65
  • 86
nomoreabond2017
  • 150
  • 2
  • 5
1

Going forward it is a good idea to dump the input files and the error messages you saw when you submit the question to SO.

Based on my investigation two things are happening:

The first more important one is the usage of input. When you use input, python actually tries to evaluate the inputted value. So you should have seen an error like this:

Username: ababababbaba
NameError: "name 'ababababbaba' is not defined"

To fix this, use raw_input instead and you should no longer have this problem. See this SO answer here for more details https://stackoverflow.com/a/4960216/4765841

The second potential problem depends on what the contents of the input file (username.txt and password.txt). If you have a new line character (\n) then when you read the line you would actually get 'myusername\n' which will not match the user's input of myusername. To fix this, you must strip the \n from the string before saving it to your usrn_read and passw_read variables.

This is what the whole thing should look like now:

def login():
    with open("username.txt","r") as username:
        usrn_read = username.read().strip("\n")
        username.close()

    with open("password.txt","r") as password:
        passw_read = password.read().strip("\n")
        password.close()

    inp_usern = raw_input("Username: ")
    inp_passw = raw_input("Password: ")
    if inp_usern==usrn_read and inp_passw==passw_read:
        print("Succesful!")
        variable.open("database.txt","a")
        variable.write("Login succesful for: "+inp_usern)
        variable.write("Password: "+inp_passw)
        print("Your username and password is:")
        print(inp_usern)
        print(inp_passw)
        forward()
    else:
        print("Not valid input. Please try again.")
        inp_usern = []
        inp_passw = []
        login()

def end():
    print("Thankyou!")

def forward():
    print("This would continue to quiz!")

login()

Addendum, this answer assumes that variable.open is valid. Otherwise I think you need to change that line to:

    with open("database.txt","a") as variable:
        variable.write("Login succesful for: "+inp_usern)
        variable.write("Password: "+inp_passw)
Kaiser Dandangi
  • 230
  • 2
  • 8
  • Pretty sure this is working now, so I’ll just check with the whole program. Thanks! Hugely appreciated. – defpython Nov 18 '17 at 16:54
  • No problem @defpython. Would you mind accepting the answer if this solved your problem? – Kaiser Dandangi Nov 18 '17 at 19:38
  • Almost worked, but could you check the edit on the question to see where I’ve gone wrong? Now it can read the first username and password that’s all... Thanks!! – defpython Nov 19 '17 at 18:00
1

i'm working on python 2.7

i used raw_input instead of input -> refer to this What's the difference between raw_input() and input() in python3.x?

also the variable "variable" needed to be assigned first

def login():
    with open("username.txt","r") as username:
        usrn_read = username.read()
        username.close()

    with open("password.txt","r") as password:
        passw_read = password.read()
        password.close()

    inp_usern = raw_input("Username: ")
    inp_passw = raw_input("Password: ")
    if inp_usern==usrn_read and inp_passw==passw_read:
        print("Succesful!")
        variable = open("database.txt","a")
        variable.write("Login succesful for: "+inp_usern)
        variable.write("Password: "+inp_passw)
        print("Your username and password is:")
        print(inp_usern)
        print(inp_passw)
        forward()
    else:
        print("Not valid input. Please try again.")
        inp_usern = []
        inp_passw = []
        login()

def end():
    print("Thankyou!")

def forward():
    print("This would continue to quiz!")

login()
DisplayName
  • 87
  • 1
  • 9
1

Because the username/password does not equal the three lines of the file combined.

bob12
alexi90
john08

You need to split the file into separate parts: bob12, alexi90, john08, on the \n. Then, throw it away: .replace('\n', '').

Kenny Chau
  • 106
  • 10
  • How would I do this? I've got it so it now works with all lines combined, but what would it look like implemented in the code? (So the input can match any line of the text file on its own.) Thanks! – defpython Nov 19 '17 at 09:57