In: Computer Science
Create a function named getCreds with no parameters that will prompt the user for their username and password. This function should return a dictionary called userInfo that looks like the dictionaries below: # Administrator accounts list adminList = [ { "username": "DaBigBoss", "password": "DaBest" }, { "username": "root", "password": "toor" } ] Create a function named checkLogin with two parameters: the userInfo and the adminList. The function should check the credentials to see if they are contained within the admin list of logins. The function should set a variable loggedIn to True if the credentials are found in the admin list, and set the variable to False otherwise. Now that we know how to check to see if a user is logging in with admin credentials, let's set up the part of the system that will continue to prompt the user for their username and password if they didn't enter correct admin credentials before. Create a while loop that will continue to call getCreds and checkLogin until a user logs in with admin credentials. After each call of checkLogin in the while loop, print to the terminal the string "---------". Once the user logs in with admin credentials, print to the terminal the string "YOU HAVE LOGGED IN!". Run the code often as you write and test individual functions with correct and incorrect admin credentials to make sure you're on the right path!
'''
Python version : 2.7
Python program to simulate user login
'''
# function to get user credentials and return it as a dictionary userInfo
def getCreds():
username = raw_input('Username : ')
password = raw_input('Password : ')
userInfo = {"username":username,"password":password}
return userInfo
# function to check if the user was able to login as admin i.e check the user credentials matches one of the credentials in adminList
def checkLogin(userInfo,adminList):
loggedIn = False
# loop over adminList
for user in adminList:
# check if username and password matches
if((userInfo["username"] == user["username"]) and (userInfo["password"] == user["password"])):
loggedIn = True # set loggedIn to True if it matches
break
return loggedIn # return loggedIn
# main program
# initialize an adminList of admin user's credentials
adminList = [ { "username": "DaBigBoss", "password": "DaBest" }, { "username": "root", "password": "toor" } ]
loggedIn = False # set loggedIn to False
# loop that continues till the user is not able to login
while not loggedIn:
userInfo = getCreds() # get user credentials
loggedIn = checkLogin(userInfo,adminList) #check user credentials
#check if user was able to login
if loggedIn:
print('YOU HAVE LOGGED IN!')
print('--------------------------------------')
#end of program
Code Screenshot:
Output: