如何正确地遍历函数中的列表

How to iterate through list in a function properly

users = [{"username":"tom", "password":1234},{"username":"pete", "password":1234},{"username":"lisa", "password":1234}]

loged_in = False

def login(log):
    while loged_in == False:
        username_check = input("Please enter your username")
        password_check = str(input("Please enter your password"))
        for user in users:
            if username_check == user["username"] and str(password_check) == str(user["password"]):
                print("Welcome, you're logged in.")
                loged_in == True
                break
            if username_check != user["username"] or str(password_check) != str(user["password"]):
                print("Wrong username or password, please try again.")
                continue

如果登录仅 succesful/unsuccesful 一次,我如何让它显示,而不是显示列表中每个项目的结果?像这样:

“用户名或密码错误,请重试。”

“用户名或密码错误,请重试。”

“用户名或密码错误,请重试。”

您的代码有几个实现错误。这是一个更简单的功能版本:

users = {"tom": 1234, "pete": 1234, "lisa": 1234}
# NB. to convert the previous list format into a dictionary, use:
# users = {d['username']: d['password'] for d in users}

def login(log):         # not sure what log is for here
    logged_in = False   # initialize in the function (or it will be True forever once a successful login is made
    while not logged_in:  # no need to compare booleans to booleans
        username_check = input("Please enter your username")
        password_check = input("Please enter your password") # no need for str conversion, input is already str
        if username_check in users and password_check == str(users[username_check]):
            print("Welcome, you're logged in.")
            logged_in = True # this and the line below are redundant
            break
        else: # no need to test the previous condition again
            print("Wrong username or password, please try again.")

将 user/pass 检查逻辑移动到它自己的方法中。这将使确定如何构造循环变得容易得多。

这是一个方法示例,如果用户名和密码有效,该方法将 return 一个用户,否则如果输入不正确,它将 return 什么都没有。

def validate_user_pass(users, username, password):
    for user in users:
        if (username, password) == (user["username"], user["password"]):
            return user
    return None

现在您的登录方法如下所示:

while True:
    username = input("Please enter your username")
    password = input("Please enter your password")
    user = validate_user_pass(users, username, password)
    if user:
        print("Welcome, you're logged in.")
        break
    else:
        print("Wrong username or password, please try again.")

创建一个变量(例如state=0)。在 for 循环中,如果登录成功,则给它 1(state=1),如果有错误,则给它一个错误编号。 根据您的状态,for 循环后打印登录结果。 类似于:

users = [{"username":"tom", "password":1234},{"username":"pete", "password":1234},{"username":"lisa", "password":1234}]

logged_in = False

def login(log):
    while logged_in == False:
        state = 0
        username_check = input("Please enter your username")
        password_check = str(input("Please enter your password"))
        for user in users:
            if username_check == user["username"] and str(password_check) == str(user["password"]):
                state = 1
                logged_in = True
                break
        if state == 1:
            print("Welcome, you're logged in.")
        else:
            print("Wrong username or password, please try again.")

或者只是:

users = [{"username":"tom", "password":1234},{"username":"pete", "password":1234},{"username":"lisa", "password":1234}]

logged_in = False

def login(log):
    while logged_in == False:
        username_check = input("Please enter your username")
        password_check = str(input("Please enter your password"))
        for user in users:
            if username_check == user["username"] and str(password_check) == str(user["password"]):
                logged_in = True
                break
        if logged_in  == False:
            print("Wrong username or password, please try again.")
    else:
        print("Welcome, you're logged in.")