简体   繁体   中英

I'm trying to create a login system using a text file

I'm trying to create a login system using a textfile so i've turned the text file into a list. When printed the textfile looks like this as i've split the rows:
[['11Ali15', '@Tyson345', 'Alice', '15', '11'], ['11Ell16', 'Nibbles', 'Ellen', '16', '11'], ['13Ell12', 'Asdan3', 'Ellie', '12', '13']]

This list is set out [Username,Password,Name,Age,Yeargroup] and i want to be able to make a log in system where username and password match so program can be continued.

The code i'm using at the moment is:(One of my subroutines)

print("--Login--")
username = input("Please enter your username:")
while len(username) == 0:
    username = input(" Please enter your username:")

password = input("Please enter your password:")
while len(password) == 0:
    password = input(" Please enter your password:")

studentlist = []
with open ("studentdetails.txt") as textfile:
    for row in textfile:
        row = row.strip ("\n")
        studentlist.append(row.split())
print (studentlist)

for everything in studentlist:       
    UserName = everything[0]
    PassWord = everything[1]

if username == UserName and password == PassWord:
    print("Logged on.")
    quizmenu()

But this doesn't work as it doesn't let u log in and goes round in a loop of asking username and password even when they're entered correctly.

Your indentation is off. It should be checking for Username and Password for each user in the text file, which would be like the following:

for everything in studentlist:       
    UserName = everything[0]
    PassWord = everything[1]

    if username == UserName and password == PassWord:
        print("Logged on.")
        quizmenu()

Suppose you have username and password data in test.txt file . You can use the below function to validate user account

def validateUserAccount():
    global index_num,userName,passWord,balances,username,password
    username=input("Enter your username : ")
    password=input("Enter your password : ")
    fileRead=open('test.txt','r')
    flag=False
    while True:
        line=fileRead.readline()
        lineLength=len(line)
        if lineLength==0:
            break
        lineItem=line.split()
        if username.strip()==lineItem[0] and password.strip()==lineItem[1] :
            print("Hello !",username," you have logged in successfully.")
            flag=True
            break
    if flag==False:
        print("The user is not found.Please re-enter your username and password.")
        validateUserAccount()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM