作业:编写登录接口
程序员文章站
2022-07-14 23:08:49
...
1. 让用户输入用户名密码
2. 认证成功后显示欢迎信息
3. 输错三次后退出程序
升级要求:
1. 可以支持多个用户登录(提示,通过列表存多个账户信息)
2. 用户三次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)
'''
基础要求:
1. 让用户输入用户名密码
2. 认证成功后显示欢迎信息
3. 输错三次后退出程序
升级要求:
1. 可以支持多个用户登录(提示,通过列表存多个账户信息)
2. 用户三次认证失败后,退出程序,再次启动程序尝试登录时,还是锁定状态(提示:需把用户锁定的状态存到文件里)
'''
version 1.0
# 1.0版本存在一些小漏洞。
user_info = {
'Catalog_Spri' : {'user_pwd' : 'C.Spridsb', 'times' : 0},
'Egon' : {'user_pwd' : 'egondsb', 'times' : 0},
'Alex' : {'user_pwd' : 'alexdsb', 'times' : 0},
}
count = 1
while count <= 3:
name = input('Please input your account>>:')
pwd = input('Please input your password>>:')
f = open('lock.txt', mode='r+', encoding='UTF-8')
is_lock = f.readlines()
if name in is_lock:
print('Your account has been locked, please try again later.')
break
if name in user_info and pwd == user_info[name]['user_pwd']:
print('Welcome to the new world.')
user_info[name]['times'] = 0
break
if (name not in user_info) or pwd != user_info[name]['user_pwd']:
print('Your account or password was wrong.')
if name in user_info:
user_info[name]['times'] += 1
if user_info[name]['times'] == 3:
f.write(name)
f.close()
count += 1
version 2.0
# 相对1.0版本,2.0版本改进了一些机制。
while True:
name = input('Please input your account>>:')
pwd = input('Please input your password>>:')
f = open('lock.txt', mode='r+', encoding='UTF-8')
is_lock = f.readlines()
if name in is_lock:
print('Your account has been locked, please try again later.')
break
if name not in user_info:
print("Your account can't be found, please try again.")
continue
if pwd == user_info[name]['user_pwd']:
print('Welcome to the new world.')
user_info[name]['times'] = 0
break
if pwd != user_info[name]['user_pwd']:
print('Your account or password was wrong, please try again.')
if name in user_info:
user_info[name]['times'] += 1
if user_info[name]['times'] == 3:
f.write(name)
break
f.close()
version 2.1
# 2.1版本做了一些优化,代码更简单,逻辑更严谨
user_info = {
'Catalog_Spri' : {'user_pwd' : 'C.Spridsb', 'times' : 0},
'Egon' : {'user_pwd' : 'egondsb', 'times' : 0},
'Alex' : {'user_pwd' : 'alexdsb', 'times' : 0},
}
while True:
name = input('Please input your account>>:')
pwd = input('Please input your password>>:')
f = open('lock.txt', mode='r+', encoding='UTF-8')
is_lock = f.readlines()
if name in is_lock:
print('Your account has been locked, please try again later.')
break
if name not in user_info:
print("Your account can't be found, please try again.")
continue
if pwd == user_info[name]['user_pwd']:
print('Welcome to the new world.')
user_info[name]['times'] = 0
break
if pwd != user_info[name]['user_pwd']:
user_info[name]['times'] += 1
if user_info[name]['times'] == 3:
f.write(name)
print('Three times wrong, you account is locked.')
break
print('Your password was wrong, please try again.')
f.close()
version 2.2
# 2.2版本增加了密码前后误加空格机制
user_info = {
'Catalog_Spri' : {'user_pwd' : 'C.Spridsb', 'times' : 0},
'Egon' : {'user_pwd' : 'egondsb', 'times' : 0},
'Alex' : {'user_pwd' : 'alexdsb', 'times' : 0},
}
while True:
name = input('Please input your account>>:')
pwd = input('Please input your password>>:')
f = open('lock.txt', mode='r+', encoding='UTF-8')
is_lock = f.readlines()
if name in is_lock:
print('Your account has been locked, please try again later.')
break
if name not in user_info:
print("Your account can't be found, please try again.")
continue
pwd = pwd.strip(' ')
if pwd == user_info[name]['user_pwd']:
print('Welcome to the new world.')
user_info[name]['times'] = 0
break
if pwd != user_info[name]['user_pwd']:
user_info[name]['times'] += 1
if user_info[name]['times'] == 3:
f.write(name)
print('Three times wrong, you account is locked.')
break
print('Your password was wrong, please try again.')
f.close()
上一篇: 纯前端Jquery分页
下一篇: asp.net自制分页