Ad

Password Function With A While Loop

- 1 answer

Hello i have a code here where i have a while loop for a password function. the function is working i just can't end the loop when a correct password is typed in

# Administrator accounts list
adminList = [
    {
        "username": "DaBigBoss",
        "password": "DaBest"
    },
    {
        "username": "root",
        "password": "toor"
    }
]


# Build your login functions below
def getCreds():
    username = input("What is your username? ")
    password = input("What is your password? ")



    return {"username": username, "password": password}



def checkLogin(adminList, user_info):
    if user_info in adminList:
        loggedIn = True
        print("------")
        print("YOU HAVE LOGGED IN!")


    else: 
        loggedIn = False
        print("------")
        print("Login Failed")

        return

logged = False

while not logged:
    user_info = getCreds()
    is_admin = checkLogin(adminList, user_info)
    if is_admin:
        logged = True

if i type a correct password i get my outcome that i have been logged in. but the loop will not end

outcome

What is your username? d
What is your password? d
------
Login Failed
What is your username? root
What is your password? toor
------
YOU HAVE LOGGED IN!
What is your username?
Ad

Answer

You need to modify you functions:

def checkLogin(adminList, user_info):
    if user_info in adminList:
        ...
        return True
    else:
        ...
        return False

while not logged:
    ...
    if is_admin:
        break
Ad
source: stackoverflow.com
Ad