#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/5/6 0006 12:22
# @Author : Anthony.Waa
# @Site :
# @File : 用户登录(三次错误机会).py
# @Software: PyCharm
# demo1
# 定义用户列表
user_list = [
{'username': 'anthony', 'password': '123'},
{'username': 'chris', 'password': '123'},
{'username': 'alex', 'password': '123'},
{'username': 'oldboy', 'password': '123'},
]
# 定义次数
count = 0
while True:
# 用户名和密码
users = input("请输入你的用户名:")
pasws = input("请输入你的密码:")
# 循环用户列表
for lists in user_list:
if lists['username'] == users and lists['password'] == pasws:
print("登录成功")
exit()
else:
print("登录失败,请重新登陆.")
count += 1
break
if count == 3:
print("你的用户名或密码,错误输入超过3次,退出登陆")
exit()
# ======================================================================================
# demo2
# 定义用户列表
user_list = [
{'username': 'anthony', 'password': '123'},
{'username': 'chris', 'password': '123'},
{'username': 'alex', 'password': '123'},
{'username': 'oldboy', 'password': '123'},
]
# 定义次数
count = 0
# 定义状态
flag = False
while True:
# 用户名和密码
users = input("请输入你的用户名:")
pasws = input("请输入你的密码:")
# 循环用户列表
for lists in user_list:
if lists['username'] == users and lists['password'] == pasws:
flag = True
else:
pass
# 判断登陆状态
if flag:
print("登录成功")
exit()
else:
print("登录失败,请重新登陆.")
count += 1
flag = False
# 判断是否失败次数超过3次
if count == 3:
print("你的用户名或密码,错误输入超过3次,退出登陆")
exit()
====================================================================
# 用户名和密码加密3次登录验证
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/7/8 0008 11:44
# @Author : Anthony.Waa
# @Site :
# @File : auth.py
# @Software: PyCharm
import hashlib
from conf import settings
def login(): # 用户登录
count_end = 3
counts = 1
while True:
username = input('请输入用户名:').strip()
password = input('请输入密码:').strip()
verifys = verify(username,password)
if verifys == True:
print('登录成功')
exit()
else:
count_end -= 1
print('账户名或密码输入错误,请重新输入...')
print('还剩%s次输入机会...'%count_end)
if counts == 3:
print('账户或密码输入错误超过3次')
exit()
counts += 1
def verify(username,password): # 用户名和密码验证
md5_obj = hashlib.md5(username.encode('utf-8'))
md5_obj.update(password.encode('utf-8'))
with open(settings.userinfo_path, 'r') as read_verify:
for userinfo_line in read_verify:
if md5_obj.hexdigest() == userinfo_line.strip():
return True
def register(): # 用户注册
while True:
print('提示:用户名必须小于8位,密码必须小于12位')
username = input('请输入用户名:').strip()
password = input('请输入密码:').strip()
if 1 < len(username) <=8 and 1 < len(password) <= 12:
md5_obj = hashlib.md5(username.encode('utf-8'))
md5_obj.update(password.encode('utf-8'))
verifys = verify(username, password) # 验证密码和用户名是否存在
if verifys == True:
print('用户名已存在,请重新输入...')
else:
with open(settings.userinfo_path,'a+') as write_register:
write_register.write(md5_obj.hexdigest()+'\n')
print('注册成功')
judge_exit = input('是否继续注册(Y/N):').strip()
if judge_exit.lower() == 'y':
continue
elif judge_exit.lower() == 'n':
print('退出成功')
exit()
else:
print('输入错误')
else:
print('用户名或密码位数不符合规定...')
continue
login()
register()