我正在尝试使用文本文件创建登录系统

时间:2017-11-20 20:43:14

标签: python list login

我尝试使用文本文件创建登录系统,因此我将文本文件转换为列表。打印时,文本文件看起来像我分割行:
[['11Ali15', '@Tyson345', 'Alice', '15', '11'], ['11Ell16', 'Nibbles', 'Ellen', '16', '11'], ['13Ell12', 'Asdan3', 'Ellie', '12', '13']]

此列表设置为[用户名,密码,姓名,年龄,年份组],我希望能够在用户名和密码匹配的系统中建立登录,以便继续进行程序。

我目前使用的代码是:(我的一个子程序)

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()

但这并不起作用,因为它不会让你登录并循环询问用户名和密码,即使他们输入正确。

2 个答案:

答案 0 :(得分:0)

你的缩进是关闭的。它应该检查文本文件中每个用户的用户名和密码,如下所示:

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

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

答案 1 :(得分:0)

假设您在test.txt文件中具有用户名和密码数据。您可以使用以下功能来验证用户帐户

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()