python无法获取loggin密码脚本

时间:2018-09-26 23:47:36

标签: python

我正在构建一个python脚本来检查文本文件中的用户名和密码,以及是否正确记录了用户。将有两个用户,并且该文件将有多个用户。 我的脚本可以工作,但是密码检查存在一些问题。我想让该功能回绕它,密码输入错误。 如果我在函数中执行此操作以使其陷入同一循环,那么只能通过退出该函数并再次调用来使其工作

下面是起作用的脚本。如果密码不正确,将返回0,0,因此将调用该函数。

user1Status = 0
user2Status = 0

def logging():
    filex = open('users.txt', 'r')
    x = filex.read()  # This will store the contents of the file as a string
    filex.close()
    login_info = x.split()
    numberUsers = (len(login_info))
    username = input("enter username")
    count = 0
    for x in range(0, numberUsers):
        if username != login_info[x]:
            count = count + 1
    if numberUsers == count:
        print('Username not recognised')
        return 0, 0
    password = input("enter password")
    for x in range(0, numberUsers):
        if username == login_info[x] and password == login_info[x + 1]:
            status = 1
            return username, status
        else:
            print('Your password is incorrect')
            #logging()
            return 0, 0

while 1:
    if user1Status == 0:
        print ('User1 please logging')
        username1, user1Status = logging()
    if user1Status == 1 and user2Status == 0:
        print('User2 please logging')
        username2, user2Status = logging()
    if (user1Status == 1) and (user2Status == 1):
        print("Welcome back %s and %s" % (username1, username2))#

如果我在日志记录函数的末尾注释掉了返回0,0和取消注释logging(),则如果输入了错误的密码然后输入了正确的密码,它将继续在该函数周围循环。这是无法通过这种方式实现的吗?

我尝试这样做的原因是。如果密码输入错误,我不想问用户是否再次拥有一个帐户。

1 个答案:

答案 0 :(得分:1)

我设法使用以下脚本使其工作。我添加了一个if语句来检查密码。这是完整的代码

user1Status = 0
user2Status = 0

def logging():
    status = 0
    attempt = 0
    filex = open('users.txt', 'r')
    x = filex.read()  # This will store the contents of the file as a string
    filex.close()
    login_info = x.split()
    numberUsers = (len(login_info))
    username = input("enter username")
    count = 0
    for x in range(0, numberUsers):
        if username != login_info[x]:
            count = count + 1
    if numberUsers == count:
        print('Username not recognised')
        return 0, 0
    while (attempt!=3):
        password = input("enter password")
        for x in range(0, numberUsers):
            if username == login_info[x] and password == login_info[x + 1]:
                status = 1
        if status == 1:
            return username, status
        else:
            print("Password incorrect")
            attempt = attempt+1
        if (attempt == 3):
            print('you have used max attempts to enter password')
            return 0, 0

def register():
    username = input('Enter a user name: ')
    password = input('Enter a password: ')
    fw = open('users.txt', 'a')
    fw.write('\n')
    fw.write(username)
    fw.write(' ')
    fw.write(password)
    fw.close()
    return(username, 0)

def start():
    q1 = input("Do you have an account?")
    if q1.lower() == "yes":
        x, y = logging()
        return (x, y)
    if q1.lower() == "no":
        x, y = register()
        return (x, y)
    else:
        print ("please enter yes or no")
        return (0,0)

while 1:
    if user1Status == 0:
        print ('User1 please logging')
        username1, user1Status = start()
    if user1Status == 1 and user2Status == 0:
        print('User2 please logging')
        username2, user2Status = start()
    if (user1Status == 1) and (user2Status == 1):
        print("Welcome back %s and %s" % (username1, username2))
        print("")
        print("1: role dice")
        print("2: user1 Logout")
        print("3: user2 Logout")
        q2 = input("Select from one of the options above: ")
        if q2 == '2':
            print("User 1 has logged out")
            user1Status = 0
        if q2 == '3':
            print("User2 has logged out")
            user2Status = 0