简体   繁体   English

如何正确地遍历 function 中的列表

[英]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

How do I make it show if the log in was succesful/unsuccesful only once, instead of showing the result for every item on the list?我如何让它显示登录是否成功/不成功一次,而不是显示列表中每个项目的结果? Like this:像这样:

"Wrong username or password, please try again." “用户名或密码错误,请重试。”

"Wrong username or password, please try again." “用户名或密码错误,请重试。”

"Wrong username or password, please try again." “用户名或密码错误,请重试。”

You code had several implementation errors.您的代码有几个实现错误。 Here is a simpler functional version:这是一个更简单的功能版本:

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.")

Make a variable (eg state=0).创建一个变量(例如state=0)。 In the for loop if the login was successful then give it 1 (state=1) if there was an error give it a number for the error.在 for 循环中,如果登录成功,则给它 1(state=1),如果有错误,则给它一个错误编号。 Based on your state, after for loop print a result of login.根据你的state,for循环后打印登录结果。 something like:就像是:

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.")

or just:要不就:

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.")

Move the user/pass checking logic into it's own method.将用户/通过检查逻辑移动到它自己的方法中。 This will make it much easier to determine how to structure the loop.这将使确定如何构建循环变得容易得多。

Here's an example of a method that will return a user if the username and password is valid, and otherwise it will return nothing if the inputs are incorrect.下面是一个方法示例,如果用户名和密码有效,该方法将返回一个用户,否则如果输入不正确,它将不返回任何内容。

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

Now your login method can look like this:现在您的登录方法可能如下所示:

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.")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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