模擬用戶登錄 ...
模擬用戶登錄
# data.json 文件同目錄下 [ { "id": 1, "username": "zhangshan", "password": "123qwe", "lock": false }, { "id": 2, "username": "lisi", "password": "123qwe", "lock": false }, { "id": 3, "username": "wangwu", "password": "123qwe", "lock": false } ]
import json # 引入模塊 count = 1 # 打開一個json文件 data = open("./data.json", encoding='utf-8') # 轉換為python對象 strJson = json.load(data) flag = False lockFlag = False while count <= 3: username = input("請輸入用戶名:") password = input('請輸入密碼:') for user in strJson: if username == user['username'] and password == user['password']: if not user['lock']: flag = True else: lockFlag = True break if flag: print('登錄成功!!') break else: print('賬號或密碼錯誤') count = count + 1 # 輸出(註意多層結構) # print(strJson[0]['id']) # for item in strJson: # print(item)