编者按:
README:此代码为用户登陆界面,添加了寻求帮助选项。
1.学习了基本数据类型,string, int,以及while循环,continue, break, if, elif, else条件语句,“x".format(x)变量替代
2.上网搜的dictionary用法,由于用的是python3.6,老教程不太管用,尝试了2天方才悟出正确的增加键值用法,希望技术大牛留言给予更多启发!
1 dict_profile = {"Alex":"alex1234", "Bob":"bob123", "Cathy":"cathy1234", 2 "David":"david12345", "Eva":"eva123"} 3 #Save the rightful username and password in the dictionary 4 black_list = { } #Create a list for not exist username 5 email = "send an email to our help desk for further solutions" 6 chance = 6 #After 3 valid trys the system will be locked 7 username = input("username:") 8 password = input("password:") 9 #password = getpass.getpass("password:") 10 while chance > 0: 11 if username not in dict_profile.keys(): #Search for username in the profile 12 black_list["{}".format(username)]="{}".format(password) 13 username = input("The username does not exist, please try again:") 14 chance = chance - 1 15 if username in black_list.keys(): #Run a security check 16 username = input("The username does not exist and has been locked, please try other combinations:") 17 chance = chance - 2 # Deduct 2 chances to punish whoever abuses the system 18 elif password != dict_profile.get(username,""): #Find the value of password to match with the username 19 print("The password does not match the username, do you have problem with memorizing things?") 20 customized_option = input("Y/N") 21 if customized_option != "N": 22 print("Please ask your partner or {}.".format(email)) 23 break 24 else: 25 password = input("Please enter your password with caution:") 26 chance = chance - 1 27 else: 28 print("Welcome my lord {_name}! Please wait while login...".format(_name = username)) 29 break 30 if chance <= 0: 31 print("The system is locked since you have too many incorrect entries!Please {}.".format(email))