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!