登录系统无限循环错误

时间:2016-10-07 21:19:56

标签: python

我自己编写一个"简单"登录系统一切正常,直到LOGIN部分。

最后while循环只发生一次,即使我放了另一个用户而不是我运行程序时创建的。我在某个时候让它工作了,但是另一个问题是while循环会一次又一次地发生。

import re
users = {}
status = ""


while status != "r":
    status = input("Press R to register!\n")

    if status == "r":
        createUser = input("Create your Username: ")
        while len(createUser)< 5:                                   #checks user len
            print("Username should contain at least 5 characters!")

            createUser = input("Create your Username: ")            #repeat if user len < 5

        while not re.match("^[a-z]*$" , createUser):                   # denies blank spaces
            print("Cannot use blank spaces")
            createUser = input("Create your Username: ")

        if createUser in users:
            print("Username already used!")
        else:
            createPass = input("Create your Password: ")
            while len(createPass) < 5:                              #checks pass len
                print("Password too short!\n          Password should contain at least 5 characters!\n")
                createPass = input("Create your Password: ")        #repeat if pass len < 5

            while not re.match("^[a-z]*$", createPass):  # denies blank spaces
                print("Cannot use blank spaces")
                createPass = input("Create your Password: ")


            else:
                users[createUser] = createPass  #adds user and pass to users
            print("User created!")

        #LOGIN
            for createUser in users:
                username = input("Username: ")
                if username == createUser in users:
                    password = input("Password: ")
                else:
                    while username != createUser:
                        print("User unregistered! Please register!")
                        createUser = input("Create your Username:")

2 个答案:

答案 0 :(得分:1)

试试这个:

def login():
    username = input("Username: ")

    if username not in users:
        print("User unregistered! Please register!")
        register()
        return

    password = input("Password: ")

    if users[username] != password
        print("Password invalid")

我在这里重写了你的代码。请注意我是如何将其分解为完成一件事的函数的:

  • usernameValidator
  • passwordValidator
  • getUsername
  • getPassword来
  • 注册
  • 登录

该计划的开始:

import re
users = {}

现在我们定义一些验证器来检查用户名/密码是否正确:

def usernameValidator(username):
    errorMessage = ""
    if len(username) < 5:
        errorMessage += "Username should contain at least 5 characters!\n"
    if not re.match("^[a-z]*$" , username):       # Note... this checks for more than just blank spaces!
        errorMessage += "Cannot use blank spaces\n"
    if username in users:
        errorMessage += "Username already used!\n"
    return errorMessage

def passwordValidator(password):
    errorMessage = ""
    if len(password) < 5:
        errorMessage += "Password should contain at least 5 characters!\n"
    if not re.match("^[a-z]*$" , password):       # Note... this checks for more than just blank spaces!
        errorMessage += "Cannot use blank spaces\n"
    return errorMessage

现在我们编写与用户交谈的getUsername / getPassword函数:

def getUsername():
    username = input("Create your Username: ")
    errorMsg = usernameValidator(username)
    print(errorMsg)

    return username if errorMsg == "" else ""

def getPassword():
    password = input("Create your Password: ")
    errorMsg = passwordValidator(password)
    print(errorMsg)

    return password if errorMsg == "" else ""

总而言之,我们写了注册/登录:

def register():
    username = ""
    password = ""

    while username == "":
        username = getUsername()

    while password == "":
        password = getPassword()

    users[username] = password
    print("User created!")

def login():
    username = input("Username: ")

    if username not in users:
        print("User unregistered! Please register!")
        register()
        return

    password = input("Password: ")

    if users[username] != password:
        print("Password invalid")

最后,我们可以运行:

while True:
    status = input("Press R to register!\nPress L to login\n")

    if status.lower() == "r":
        register()
    if status.lower() == "l":
        login()

Try it online

答案 1 :(得分:1)

首先,"^[a-z]*$"测试没有小写字母,但这并不意味着“没有空格”,所以我已经为"\\s+"

更正了

你真的需要了解方法并解决问题。

1)询问用户名

def get_username():
    while True:
        uname = input("Create your Username: ")
        if len(uname) < 5:
            print("Username should contain at least 5 characters!")
            continue
        if re.search("\\s+", uname):
            print("Cannot use blank spaces")
            continue
        break # input successful
    return uname

2)要求输入密码

def get_password():
    while True:
        passwd = input("Create your Password: ")
        if len(passwd) < 5:
            print("Password too short!\n\tPassword should contain at least 5 characters!\n")
            continue
        if re.search("\\s+", passwd):
            print("Cannot use blank spaces")
            continue
        break # input successful
    return passwd

3)注册

def register():
    while True:
        uname = get_username()
        if uname not in users:
            break  # continue to get password
        else:
            print("Username already used!")

    passwd = get_password()
    if passwd:
        users[uname] = passwd
    print("User created!")

4)尝试登录(最多3次尝试)。目前还不清楚你想如何运行这个......

def login():
    for i in range(3):
        username = input("Username: ")
        if username in users:
            password = input("Password: ")
            if users[username] != password:
                print("Wrong username or password")
        else:
            print("User does not exist")
    else:
        print("Max attempts reached")

5)(可选)了解D.R.Y,因为您的测试基本相同

def test_input(s):
    if len(s) < 5:
        print("Input should contain at least 5 characters!")
        return False
    if re.search("\\s+", s):
        print("Cannot use blank spaces")
        return False
    return True

6)运行一些使用所有这些方法的代码。

status = input("Press R to register!\n")
if status.lower() == "r":
    register()