Day12作業(yè)

登錄注冊功能

import file_manager

'''注冊'''
def register():
    while True:
        name=input("請輸入用戶名:")
        if 3<=len(name)<=6:
            break
        else:
            print("用戶名錯誤,請重新輸入")

    while True:
        passwd=input("請輸入密碼:")
        if 6<=len(passwd)<=12:
            break
        else:
            print("密碼錯誤,請重新輸入")


    content=file_manager.read_json_file("file/register.json")
    if content == None:
        content={}
    if name in content:
        print("注冊失敗,%s已經(jīng)注冊"%name)
        return
    else:
        content[name]=passwd
        file_manager.write_json_file("file/user_info.json",content)
        print("注冊成功")
def login():
    # 1.輸入賬號和密碼
    name = input('請輸入賬號:')
    passwd = input('請輸入密碼:')

    # 2.判斷賬號是否注冊過
    all_user = file_manager.read_json_file('files/user_info.json')
    if not all_user:
        print('登錄失敗!賬號沒有注冊!')
        return

    if name in all_user:
        if all_user[name] == passwd:
            print('登錄成功!')
            # 進(jìn)入學(xué)生管理頁面

            # student_manager.show_manage_page()
        else:
            print('登錄失??!密碼錯誤!')
    else:
        print('登錄失敗!賬號沒有注冊!')


# 顯示學(xué)生管理主頁面
# def show_page_index():
#     return
choose=0
while True:
    show_content=file_manager.read_txt_file("file/page_index.txt")
    print(show_content)
    choose=input("請選擇1-3:")
    # print(choose,type(choose))

    if choose=="1":
        register()
    elif choose=="2":
        login()
    else:
        print("返回")
    break

文件操作函數(shù)封裝

#封裝文件操作函數(shù)
import json
def read_txt_file(file:str):
    """
    讀取普通文件內(nèi)容的函數(shù)
    file:文件路徑
    :return:返回文件內(nèi)容
    """
    try:
        with open(file,"r",encoding="utf-8") as f:
            content=f.read()
            return content
    except FileNotFoundError:
        print("文件不存在")
        return ''


def read_json_file(file):
    """
    讀取json文件內(nèi)容的函數(shù)
    file:文件路徑
    :return:返回文件內(nèi)容
    """
    try:
        with open(file,"r",encoding="utf-8") as f:
            return json.load(f)

    except FileNotFoundError:
        print("文件不存在")
        return None

def write_json_file(file,obj):

    with open(file,"w",encoding="utf-8") as f:
        return json.dump(file,obj)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容