close

#----載入模組 io,ast,os  開始

from io import open
import ast,os

#----載入模組 io,ast  結束


data=dict()  #創建帳密資料的空字典

main()  # 執行主程式


#-----主程式    開始
def main():  
    global data
    data=readData()
    while True:
        menu()
        choice=input("請輸入您的選擇:")
        print()
        if choice.isdigit()==True and choice !="":
            a={1:input_data,2:disp_data,3:edit_data,4:delete_data,0:exit_program}.get(int(choice),exit_program)
            a()
#-----主程式    結束

#-------主選單  開始

def menu():
    os.system("cls")  #利用os.system執行命令 cls清除畫面
    print("帳號、密碼管理系統")
    print("-"*20)
    print("1.輸入帳號、密碼")
    print("2.顯示帳號、密碼")
    print("3.修  改  密  碼")
    print("4.刪除帳號、密碼")
    print("0.結  束  程  式")
    print("-"*20)

#-------主選單  結束

#-------讀取帳密文字檔   開始

def readData():
    with open('id_pw.txt',mode='r',encoding='utf8') as f:
        data=f.readline()
        if data !="":
            return ast.literal_eval(data)  #ast.literal_eval 可以將字串轉成字典的函式
        else:
            return {}

#-------讀取帳密文字檔   結束

#-------副程式  輸入帳密介面  開始

def input_data():
    while True:
        name=input("請輸入帳號(Enter-->停止輸入):")
        if name =="":break
        if name in data:
            print("{}帳號已經存在!".format(name))
            continue
        password=input("請輸入密碼(Enter-->停止輸入):")
        data[name]=password
        with open('id_pw.txt',mode='w',encoding='utf8') as f:
            f.write(str(data))
            input("{}已經儲存完畢".format(name))
            break

#-------副程式  輸入帳密介面  結束

 

#-------副程式  顯示所有帳密  開始
def disp_data():

    print ("{:10}\t{:10}".format("帳號","密碼"))
    print("="*20)
    for key in data:
        print("{:10}\t{:10}".format(key,data[key]))
    input("按任意鍵返回主選單")  #使用input可以讓畫面暫時凍結,否則又會立即跳至主選單,而看不到剛才執行的結果
#-------副程式  顯示所有帳密  結束
 

#-------副程式  編輯帳密  開始
def edit_data():
    while True:
        name=input("請輸入要修改的帳號,(Enter-->停止輸入):")
        if name =="":break
        if not name in data:
            print("{}帳號不存在!".format(name))
            continue
        print("舊密碼是:{}".format(data[name]))
        password=input("請輸入新密碼:")
        data[name]=password
        with open('id_pw.txt',mode='w',encoding='utf8') as f:
            f.write(str(data))  #務必要將字典再次轉為字典,否則存至txt檔會產生錯誤
            input("密碼更改完畢,請按任意鍵回主選單")
            break

#-------副程式  編輯帳密  結束

 

#-------副程式  刪除帳密  開始
def delete_data():
    while True:
        name=input("請輸入要刪除的帳號(Enter-->停止輸入)::")
        if name=="":break
        if not name in data :
            print("{}這個帳號不存在,無法刪除".format(name))
            continue
        del data[name]
        input("{}帳號已刪除,請按任意鍵回主選單".format(name))
        break

#-------副程式  編輯帳密  結束

#-------副程式 主程式  結束

def exit_program():
    exit()

 

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 吳盈霖 的頭像
    吳盈霖

    吳盈霖的部落格

    吳盈霖 發表在 痞客邦 留言(0) 人氣()