python学习0313作业

#一:今日作业:
#1、编写文件copy工具
# src_file = input("请输入源文件路径:").strip()
# des_file = input("请输入目标文件路径:").strip()
# with open(r'{}'.format(src_file),mode='rt',encoding="utf-8") as f1,
#         open(r'{}'.format(des_file),mode='wt',encoding="utf-8") as f2:
#     res1 = f1.read()
#     f2.write(res1)
# print(res1)

#2、编写登录程序,账号密码来自于文件
# username = input("请输入您的账号:").strip()
# password = input("请输入密码:").strip()
# with open(r'user.txt',mode='rt',encoding="utf-8") as f:
#     for line in f:
#         user,pwd = line.strip().split(":")
#         if user == username and pwd == password:
#             print("登录成功!!")
#             break
#     else:
#         print("登录失败!!!")

#3、编写注册程序,账号密码来存入文件
# username = input("请输入注册账号:").strip()
# password = input("请输入注册密码:").strip()
# with open(r'user2.txt',mode='at',encoding='utf-8') as f:
#     f.write('{}:{}
'.format(username,password))
#二:周末综合作业:
# 2.1:编写用户登录接口
#1、输入账号密码完成验证,验证通过后输出"登录成功"
#2、可以登录不同的用户
#3、同一账号输错三次锁定,(提示:锁定的用户存入文件中,这样才能保证程序关闭后,该用户仍然被锁定)

count = 1
while count <= 3:
    username = input("请输入您的账号:").strip()
    password = input("请输入密码:").strip()
    with open(r'user.txt', mode='rt', encoding="utf-8") as f1:
        for line in f1:
            user, pwd = line.strip().split(":")
            if user == username and pwd == password:
                print("登录成功!!")
                count = 4
                break
        else:
            with open(r'user.txt', mode='at', encoding='utf-8') as f2:
                f2.write('{}:{}
'.format(username, password))
                print("您输入的账号或者密码错误,请重新输入")
    count += 1
with open(r'user.txt', mode='rt', encoding="utf-8") as f3:
    for i in f3.readlines():
        print(i)
# 2.2:编写程序实现用户注册后,可以登录,
# 提示:
# while True:
#     msg = """
#     0 退出
#     1 登录
#     2 注册
#     """
#     print(msg)
#     cmd = input('请输入命令编号>>: ').strip()
#     if not cmd.isdigit():
#         print('必须输入命令编号的数字,傻叉')
#         continue
#     if cmd == '0':
#         print("退出")
#         break
#     elif cmd == '1':
#         # 登录功能代码(附加:可以把之前的循环嵌套,三次输错退出引入过来)
#         count = 1
#         while count <= 3:
#             username = input("请输入您的账号:").strip()
#             password = input("请输入密码:").strip()
#             with open(r'user.txt', mode='rt', encoding="utf-8") as f:
#                 for line in f:
#                     user, pwd = line.strip().split(":")
#                     if user == username and pwd == password:
#                         print("登录成功!!")
#                         count = 4
#                         break
#                 else:
#                     print("登录失败!!!")
#             count += 1
#         else:
#             print("拜拜")
#     elif cmd == '2':
#         # 注册功能代码
#         username = input("请输入注册账号:").strip()
#         password = input("请输入注册密码:").strip()
#         with open(r'user.txt', mode='at', encoding='utf-8') as f:
#             f.write('{}:{}
'.format(username, password))
#         print("已注册成功!!!")
# else:
#     print('输入的命令不存在')

# 思考:上述这个if分支的功能否使用其他更为优美地方式实现
原文地址:https://www.cnblogs.com/liunaixu/p/12488690.html