1. 程式人生 > >python全棧筆記-day04-chapter1 homework_login_system

python全棧筆記-day04-chapter1 homework_login_system

# 登陸認證程式
# 作業需求:
# 基礎需求:
# 讓使用者輸入使用者名稱密碼
# 認證成功後顯示歡迎資訊
# 輸錯三次後退出程式
#
# 升級需求:
# 可以支援多個使用者登入(提示,通過列表存多個賬戶資訊)
# 使用者3次認證失敗後,退出程式,再次啟動程式嘗試登入時,還是鎖定狀態(提示: 需把使用者鎖定的狀態存到檔案裡)

f = open('login_system.txt')
all_info = [line.strip() for line in f.readlines()]
f.close()
# print(all_info)
for index,k in enumerate(all_info):
    all_info[index] = eval(k)
# print(all_info)\

count = 0
exit_flag1 = False

while not exit_flag1:
    exit_flag2 = False
    exit_flag3 = False
    choice = input('請選擇(l(L):登陸,s(S):註冊):')
    if not choice:
        print('\033[1;0m輸入錯誤\033[0m')
    elif choice == 's' or choice =='S':
        user_info = {'username': ' ', 'password': ' ', 'count': 0}
        isexit = False
        while not exit_flag2:#判斷使用者名稱是否存在
            username = input('使用者名稱:')
            if not username:
                continue
            if len(all_info) != 0:
                for k in all_info:
                    if username == k['username']:
                        isexit = True
                        print("\033[1;0m使用者名稱已存在\033[0m")
                        break
            exit_flag2 = True
        if not isexit:
            password = input('密碼:')
            user_info["username"] = username
            user_info["password"] = password
            all_info.append(user_info)
            with open('login_system.txt', 'a') as f:#寫進檔案
                f.writelines(str(user_info)+'\n')
            print('\033[1;0m註冊成功\033[0m')
    elif choice == 'l' or choice == 'L':
        if len(all_info) == 0:#如果沒有使用者
            print('請註冊!')
            exit_flag3 = True
        while not exit_flag3:#判斷使用者名稱是否不存在
            username_exit = False
            username = input('使用者名稱:')
            for k in all_info:
                if username == k['username']:#如果使用者名稱存在
                    if k['count'] < 3:
                        k['count'] = 0#上次登入輸錯密碼次數少於3,則清零
                        temp_password = k['password']
                        while True:
                            password = input('密碼:')
                            if password == temp_password:#判斷密碼是否正確
                                print('\033[1;0m登陸成功!\033[0m')
                                break
                            else:
                                count += 1
                                k['count'] = count
                                # print(count)
                                # print(all_info)
                                with open('login_system.txt','w') as f:#將列表寫進檔案
                                    for k1 in all_info:
                                        f.writelines(str(k1)+"\n")
                                print('\033[1;0m密碼錯誤!\033[0m')
                                if count == 3:
                                    print('\033[1;0m密碼輸入錯誤已超3次!賬號已被鎖定!\033[0m')
                                    k['count'] = 3
                                    exit()
                        username_exit = True
                        exit_flag1 = True
                        exit_flag3 = True
                        break
                    else:
                        print('\033[1;0m賬號已被鎖定!\033[0m')
                        exit()
            if  not username_exit:
                print('\033[1;0m使用者名稱不存在\033[0m')
                exit_flag3 =True
    else:
        print('\033[1;0m輸入錯誤\033[0m')