代码:用户注册登录系统

代码:用户注册登录系统

import sys #待考究
from datetime import datetime
def register():
    """
    用户注册
    :return:
    pass
    """
    print("----用户注册------")
    while True:
        user_name = input("请输入用户名(N退出):")
        if user_name.upper() == "N":
            return
        user_pwd = input("请输入登录密码:")
        creat_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        with open("zhuce",'a', encoding = 'utf-8') as f:
            line = "%s|%s|%s\n"%(user_name,user_pwd,creat_time)
            f.write(line)
            
            
def login():
    """
    用户登录
    :return:
    pass
    """
    count = 0
    while True:
        print("----用户登录----")
        user_name = input("请输入用户名:")
        user_pwd = input("请输入登录密码:")
        with open("info.tet",'r',encoding = 'utf-8') as f:
            user_info = f.readlines()
            new_user_info = [item.strip() for item in user_info if item.strip()] #strip与if的搭配也要值得注意
            if user_name in new_user_info:
                print("账户已被冻结")
                return
        user_exit = False
        flag = False
        with open("data.tet","r",encoding = "utf-8") as f:
            for line in f:
                user_name1,user_pwd1,time1 = line.split("|")

                if user_name == user_name1:
                    user_exit = True

                if user_name == user_name1 and user_pwd == user_pwd1:
                    falg = True
                    print("你已经登入系统")
                    sys.exit(0)

        if not user_exit:
            print("用户名不存在,请重新输入")
            continue
        if not falg:
            print("用户名或密码错误")
        count += 1
        if count > 3:
            print("登录次数已用完,强迫退出")
            return


def run():
    """
    程序入口
    :return:
    pass
    """
    func_dict = {1:register ,2:login}
    while True:
        print("1.用户注册;2.用户登录")
        choice = int(input("请选择:"))
        func_name = func_dict.get(choice)
        func_name()
        if not func_name: #if none :值得注意与学习
            print("选择错误,请重新选择!")
if __name__ == '__main__': #只有是主文件的时候才执行,导入的时候不执行
    run()

总结:

  • sys的用法
  • %s的用法
  • 以及split的用法
posted @ 2020-08-31 12:22  小杨的冥想课  阅读(711)  评论(0编辑  收藏  举报