-2

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?
tumpk
  • 1
  • 1

1 Answers1

0

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
Zaraki Kenpachi
  • 5,510
  • 2
  • 15
  • 38
  • It's even better to throw away `is_admin` and use `logged` instead, so no break is needed: `logged= checkLogin(adminList, user_info)` – Ocaso Protal Jun 14 '19 at 07:57