Home > Mobile >  How to iterate through list in a function properly
How to iterate through list in a function properly

Time:09-27

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

CodePudding user response:

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

CodePudding user response:

Make a variable (e.g 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. Based on your state, after for loop print a result of login. 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.")

CodePudding user response:

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