3.15周末作业
1、输入账号密码完成验证,验证通过后输出"登录成功"
2、可以登录不同的用户
3、同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)
新建user.txt
import time
count=0 while count<3: in_name = input("请输入您的账号:").strip() in_pwd = input("请输入您的密码:").strip() with open('user.txt', mode='rt', encoding='utf-8') as f: for i in f: username, userpwd = i.strip().split(":") if in_name == username and in_pwd == userpwd: print('登录成功') count=3 else: print("账号或者密码错误") count += 1 if count==3: with open('users.txt',mode='at',encoding='utf-8') as f1: f1.write(in_name)
time.sleep(5) print("账号已被锁定") break
编写程序实现用户注册后,可以登录,
提示:
while True:
msg = """
0 退出
1 登录
2 注册
"""
print(msg)
cmd = input('请输入命令编号>>: ').strip()
if not cmd.isdigit():
print('必须输入命令编号的数字,傻叉')
continue
while True:
msg = """
0 退出
1 登录
2 注册
"""
print(msg)
cmd = input('请输入命令编号>>: ').strip()
if not cmd.isdigit():
print('必须输入命令编号的数字,傻叉')
continue
if cmd == '0':
break
elif cmd == '1':
in_name = input("请输入您的账号:").strip()
in_pwd = input("请输入您的密码:").strip()
with open('user.txt', mode='rt', encoding='utf-8') as f:
for a in f:
username, userpwd = a.strip().split(":")
if in_name == username and in_pwd == userpwd:
print('登录成功')
else:
print("账号或者密码错误")
elif cmd == '2':
in_name = input("请输入您的账号:").strip()
in_pwd = input("请输入您要设置的密码:").strip()
with open('user.txt', mode='at', encoding='utf-8') as f1:
f1.write('{}:{}\n'.format(in_name,in_pwd))
print('设置成功')
break
else:
print('输入的命令不存在')

浙公网安备 33010602011771号