python 用户登录程序
需求: 让用户输入用户名密码 认证成功后显示欢迎信息 输错三次后退出程序 可以支持多个用户登录 用户3次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态
# -*- coding:utf-8 -*- # author: Skyell flag = True account_list = {'xiaopang': '123', 'haohao': '234'} # 已有账号、密码 username = input('Please input your username:').strip() lock_name = open('lockname_list.txt', 'r', encoding="utf-8") for i in lock_name: lockname = i.strip() if lockname == username: print('your account has been locked!') break else: continue lock_name.close() if username != lockname: password_wrong = 0 while flag: password = input('Please input your password:').strip() acc_name = [keys for keys in account_list] # 利用列表生成式将账号信息存入列表中 if username in acc_name: if account_list[username] == password: # 判断账号、密码是否正确 print('welcome back!') flag = False break else: print('Please try again!') password_wrong += 1 if password_wrong == 3: # 密码错误3次,锁定用户 print('your account has been locked') lock_name = open('lockname_list.txt', 'a+', encoding="utf-8") lock_name.write(username+'\n') # 将被锁定的账号写入锁定文件中 lock_name.close() else: print('Account is not found') flag = False break
基本实现需求,继续继续!
数据驱动变革-云将skyell。用Flask+Nginx+uWsgi搭建的个人博客:http://www.skyell.cn/