1. 程式人生 > >python 學習_第二模塊練習題__修改個人信息

python 學習_第二模塊練習題__修改個人信息

方法 改密碼 size repl password continue int ont lis

需求:

  在一個文件裏存多個個人信息,如下 account.txt

shanshan,shanshan,杜姍姍,22,Model,PR,22
alex,abc123,Alexander Li,222,CEO,IT,1333
rain,rain,ysl,27, Engineer ,IT ,133542453453

1.輸入用戶密碼, 正確後登陸系統, 打印

    1.修改個人信息
    2.打印個人信息
    3.修改密碼
    4.退出(q)

2.每個選項寫一個方法

3.登陸是輸錯3次退出程序

#  打印 文件裏面內容
def
show_information(username): with open("account.txt","r+",encoding="utf-8") as f: for line in f: user = line.split(,) [0].strip() passwd = line.split(,) [1].strip() name = line.split(,) [2].strip() age = line.split(,) [3].strip() post
= line.split(,)[4].strip() #崗位 occupation = line.split(,)[5].strip() #職業 phone= line.split(,)[6].strip() if username == user: print( {"賬號:":user, "密碼:": passwd, "姓名:": name,
"年紀:": age, "崗位:": post, "職業:": occupation, "手機號碼:":phone })

#auth 認證
def login(username,password):
    with  open("account.txt","r+",encoding="utf-8") as f:
        for line in f:
            user = line.split(,) [0].strip()
            passwd = line.split(,) [1].strip()
            # name = line.split(‘,‘) [2].strip()
            # age = line.split(‘,‘) [3].strip()
            # post = line.split(‘,‘)[4].strip()             #崗位
            # occupation = line.split(‘,‘)[5].strip()        #職業
            # phone= line.split(‘,‘)[6].strip()
            if username == user and password==passwd:
                print(username,password)
                return  [0,username]

# 修改個人信息
def modify_information(username):
    f_new = open("account_new.txt","w",encoding="utf-8")
    f_old = open("account.txt","r+",encoding="utf-8")
    print(
        "\t2.修改姓名\n"
        "\t3.修改年紀\n"
        "\t4.修改崗位\n"
        "\t5.修改職業\n"
        "\t6.修改手機號碼\n")

    modify = int(input(" 請輸入選項:     ").strip())
    modify_num = list(range(2, 7))
    if modify not in modify_num: return 1

    for line in f_old:
        user = line.split(,)[0].strip()
        if user == username:
            infor = input(" 修改為的內容:     ").strip()
            ll = line.split(,)
            ll[modify] = infor
            ll = ,.join(ll)
            f_new.write(ll)
        else:
            new_line = line
            f_new.write(new_line)

    f_new.close()
    f_old.close()
    if  os.path.getsize("account_new.txt") !=0:
        os.replace("account_new.txt","account.txt")

# 修改密碼
def modify_passwd(username):
    f_new = open("account_new.txt", "w", encoding="utf-8")
    f_old = open("account.txt", "r+", encoding="utf-8")
    for line in f_old:
        user = line.split(,)[0].strip()
        if user == username:
            while True:
                infor = input(" 新密碼為的內容:     ").strip()
                infor_again = input(" 新密碼為的內容:     ").strip()
                if infor == infor_again:
                    line = line.split(,)
                    line[1] = infor
                    line = ,.join(line)
                    f_new.write(line)
                    break
        else:
            new_line = line
            f_new.write(new_line)
    f_old.close()
    f_new.close()
    os.replace("account_new.txt", "account.txt")

# 主函數
def  main():
    count = 0
    while count < 3:
        user = input("user:         ")
        passwd = input("passwd:     ")
        try:
            auth = login(user, passwd)[0]
            if auth==0:
                print("登錄成功")
                while True:
                    print("\n-----------有以下幾個功能選項----------------")
                    select = int(input("\t1.修改個人信息\n"
                                       "\t2.打印個人信息\n"
                                       "\t3.修改密碼\n"
                                       "\t4.退出(q)\n"
                                       "\n").strip())
                    if select == 1:
                        modify_information(user)  # ("1.修改個人信息")
                    elif select == 2:
                        show_information(user)    # ("2.打印個人信息")
                    elif select == 3:
                        modify_passwd(user)       # ("3.修改密碼")
                    elif select == 4:
                        print("4.退出(q)")
                        exit()
                    else:
                        print("輸入有誤 重新輸入")
                        continue
        except:
            print("登錄失敗。。。。")
            count +=1
            continue

python 學習_第二模塊練習題__修改個人信息