需求:模拟实现一个ATM+购物商城程序1.额度1500或者自定义2.实现购物商城,买东西加入购物车,调用信用卡接口3.可以提现,手续费5%4.支持账户登录5.支持账户间转账6.记录每日日常消费流水7.提供还款接口8.ATM记录操作日志9.提供管理接口,包括添加账户,用户额度,冻结账户等10.用户认证用装饰
1 #Author wangmengzhu 2 from auth import * 3 from creditcard import * 4 from shopping import * 5 from user import * 6 ‘‘‘ 7 主页面列表 8 ‘‘‘ 9 def main_list(): 10 msg = [‘atm‘,‘购物商城‘,‘管理系统‘] 11 index = 0 12 for i in msg: 13 print(index + 1,i) 14 index += 1 15 16 ‘‘‘ 17 atm页面列表 18 ‘‘‘ 19 def atm_list(): 20 msg = [‘信用卡信息‘,‘查看信用卡流水‘,‘信用卡还款‘,‘信用卡取现‘,‘信用卡转账‘,‘信用卡绑定‘] 21 index = 0 22 for i in msg: 23 print(index + 1,i) 24 index += 1 25 26 ‘‘‘ 27 购物车列表 28 ‘‘‘ 29 def cat(): 30 msg = [‘购物商城‘,‘查看购物记录‘,‘购物结算‘] 31 index = 0 32 for i in msg: 33 print(index + 1,i) 34 index += 1 35 36 ‘‘‘ 37 管理员列表 38 ‘‘‘ 39 def admin(): 40 print(‘冻结信用卡‘) 41 42 ‘‘‘ 43 主函数 44 ‘‘‘ 45 def main(): 46 print(‘购物商城atm系统‘.center(20,‘*‘)) 47 while True: 48 print(‘欢迎来到atm购物系统‘.center(20,‘*‘)) 49 main_list() 50 choice = input(‘请输入选择购物的ID号: ‘).strip() 51 if choice == ‘q‘: 52 print(‘欢迎下次继续登录‘) 53 exit() 54 if choice.isdigit(): 55 choice = int(choice) 56 if choice >= 1 and choice <= 3: 57 while True: 58 if choice == 1: 59 print(‘欢迎来到信用卡管理中心‘.center(20,‘*‘)) 60 atm_list() 61 atm_choice = input(‘请选择atm的ID号: ‘).strip() 62 if atm_choice.isdigit(): 63 atm_choice = int(atm_choice) 64 if atm_choice >= 1 and atm_choice <= 6: 65 while True: 66 if atm_choice == 1: 67 creditcard_data() 68 break 69 elif atm_choice == 2: 70 look_record() 71 break 72 elif atm_choice == 3: 73 repayment() 74 break 75 elif atm_choice == 4: 76 take_money() 77 break 78 elif atm_choice == 5: 79 transfer() 80 break 81 elif atm_choice == 6: 82 link_creditcard() 83 break 84 else: 85 print(‘请输入正确的ID号‘) 86 else: 87 print(‘输入的ID号不是数字,请重新输入‘) 88 elif choice == 2: 89 print(‘欢迎来到购物中心‘.center(20,‘*‘)) 90 cat() 91 shopping_choice = input(‘请输入购物商城的ID号: ‘).strip() 92 if shopping_choice.isdigit(): 93 shopping_choice = int(shopping_choice) 94 if shopping_choice >= 1 and shopping_choice <= 3: 95 while True: 96 if shopping_choice == 1: 97 shopping_mall() 98 break 99 elif shopping_choice == 2: 100 cat_record() 101 break 102 elif shopping_choice == 3: 103 pay() 104 break 105 else: 106 print(‘输入的ID号不正确,请重新输入‘) 107 else: 108 print(‘输入的ID号应该为数字‘) 109 elif choice == 3: 110 admin() 111 admin_auth() 112 link_creditcard() 113 else: 114 print(‘请输入正确的ID号‘) 115 else: 116 print(‘输入的不是数字,请重新输入‘) 117 118 main()
1 #Author wangmengzhu 2 #用户认证,信用卡认证,管理员认证 3 import os 4 import json 5 import sys 6 BASE_PATH = os.path.dirname(os.path.dirname(__file__)) 7 user_dic = BASE_PATH + r‘/db/user_data.txt‘ 8 creditcard_dic = BASE_PATH + r‘/db/creditcard_data.txt‘ 9 10 ‘‘‘ 11 认证装饰器 12 ‘‘‘ 13 def auth(auth_type): 14 def out_wrapper(func): 15 if auth_type == ‘user_auth‘:#用户认证 16 def wrapper(): 17 res = func 18 user_name = input(‘请输入登录用户名:‘).strip() 19 user_pwd = input(‘请输入登录密码: ‘).strip() 20 if len(user_name) > 0: 21 with open(user_dic,‘r‘) as f: 22 user_data = json.loads(f.read()) 23 if user_name in user_data.keys() and user_pwd == user_data[user_name][‘password‘]: 24 if user_data[user_name][‘locked‘] == False: 25 print(‘%s认证成功‘%(user_name)) 26 return res,user_name 27 else: 28 print(‘%s用户已经被锁定,认证失败‘%(user_name)) 29 else: 30 print(‘%s用户名或密码错误,认证失败‘%(user_name)) 31 else: 32 print(‘输入的用户名不能为空‘) 33 return wrapper 34 elif auth_type == ‘creditcard_auth‘:#信用卡认证 35 def wrapper(): 36 res = func 37 creditcard = input(‘请输入你的信用卡卡号(6位数字): ‘).strip() 38 password = input(‘请输入信用卡密码: ‘).strip() 39 if creditcard: 40 with open(creditcard_dic,‘r‘) as f: 41 creditcard_data = json.loads(f.read()) 42 if creditcard in creditcard_data.keys() and password == creditcard_data[creditcard][‘password‘]: 43 if creditcard_data[creditcard][‘locked‘] == False: 44 print(‘信用卡%s认证成功‘%(creditcard)) 45 return res,creditcard 46 else: 47 print(‘信用卡%s已经被锁定‘%(creditcard)) 48 else: 49 print(‘信用卡%s不存在‘%(creditcard)) 50 else: 51 print(‘信用卡号输入错误‘) 52 return wrapper 53 elif auth_type == ‘admin_auth‘: 54 def wrapper(): 55 res = func() 56 admin_dic = {‘admin‘:‘mengzhu‘,‘password‘:‘susu0525‘} 57 admin_name = input(‘请输入管理员登陆名: ‘).strip() 58 admin_psd = input(‘请输入管理员密码: ‘).strip() 59 if len(admin_name) > 0: 60 if admin_name == admin_dic[‘admin‘] and admin_psd == admin_dic[‘password‘]: 61 print(‘管理员%s登陆成功‘%(admin_name)) 62 return res,admin_name 63 else: 64 print(‘输入的管理员用户名或密码出错!‘) 65 else: 66 print(‘输入的用户名不能为空‘) 67 return wrapper 68 return out_wrapper 69 @auth(auth_type = ‘user_auth‘) 70 def user_auth(): 71 print(‘用户登录名认证‘.center(20,‘*‘)) 72 @auth(auth_type = ‘creditcard_auth‘) 73 def creditcard_auth(): 74 print(‘信用卡登陆认证‘.center(20,‘*‘)) 75 @auth(auth_type = ‘admin_auth‘) 76 def admin_auth(): 77 print(‘管理员登陆认证‘.center(20,‘*‘)) 78 if __name__ == ‘__main__‘: 79 user_auth() 80 creditcard_auth() 81 admin_auth()
1 #Author wangmengzhu 2 ‘‘‘ 3 信用卡信息 4 ‘‘‘ 5 from log import get_logger 6 import json 7 import os 8 BASE_PATH=os.path.dirname(os.path.dirname(__file__)) 9 10 ‘‘‘ 11 数据库绝对路径 12 ‘‘‘ 13 user_dic = BASE_PATH + r‘/db/user_data.txt‘ 14 creditcard_dic = BASE_PATH + r‘/db/creditcard_data.txt‘ 15 # print(creditcard_dic) 16 user_water = BASE_PATH + r‘/log/water_record.txt‘ 17 logger = get_logger() 18 ‘‘‘ 19 信用卡信息 20 ‘‘‘ 21 def creditcard_data(): 22 while True: 23 with open(creditcard_dic,‘r‘) as f: 24 creditcard_data = json.loads(f.read()) 25 choice = input(‘请输入你的信用卡号:‘).strip() 26 if choice in creditcard_data.keys(): 27 print(‘信用卡信息‘.center(20,‘*‘)) 28 print(‘信用卡号%s,个人信息%s,额度%s,可取现额度%s,可用额度%s‘%(creditcard_data[choice][‘creditcard‘], 29 creditcard_data[choice][‘personinfo‘], 30 creditcard_data[choice][‘deflimit‘],creditcard_data[choice][‘limitcash‘], 31 creditcard_data[choice][‘limit‘])) 32 elif choice == ‘q‘: 33 print(‘欢迎下次继续查询!‘) 34 break 35 else: 36 print(‘%s信用卡号不存在‘%(choice)) 37 38 # creditcard_data() 39 40 ‘‘‘ 41 查看信用卡流水 42 ‘‘‘ 43 def look_record(): 44 while True: 45 choice = input(‘请输入你要查看流水的信用卡号(退出q): ‘).strip() 46 with open(creditcard_dic,‘r‘) as f1: 47 core_record = json.loads(f1.read()) 48 if choice in core_record.keys(): 49 print(‘流水记录‘.center(20,‘*‘)) 50 with open(user_water,‘r‘) as f: 51 for i in f: 52 if choice in i: 53 print(i.strip()) 54 else: 55 print(‘信用卡号不存在‘) 56 if choice == ‘q‘: 57 break 58 59 ‘‘‘ 60 信用卡还款 61 ‘‘‘ 62 def repayment(): 63 while True: 64 print(‘信用卡还款‘.center(20,‘*‘)) 65 with open(creditcard_dic,‘r+‘) as f: 66 creditcard_data = json.loads(f.read()) 67 choice = input(‘请输入还款信用卡账号,退出请输入q: ‘).strip() 68 if choice == ‘q‘: 69 break 70 if choice in creditcard_data.keys(): 71 money = input(‘请输入还款金额: ‘).strip() 72 password = input(‘请输入密码: ‘).strip() 73 if password == creditcard_data[choice][‘password‘]: 74 if money.isdigit(): 75 money = int(money) 76 limit = creditcard_data[choice][‘limit‘] 77 limitcash = creditcard_data[choice][‘limitcash‘] 78 limit = limit + money 79 limitcash = limitcash + money 80 creditcard_data[choice][‘limit‘] = limit 81 creditcard_data[choice][‘limitcash‘] = limitcash 82 dic = json.dumps(creditcard_data) 83 repayment_data = [str(choice),‘信用卡还款‘,str(money)] 84 msg = ‘****‘.join(repayment_data) 85 f.seek(0) 86 f.write(dic) 87 logger.debug(msg) 88 print(‘%s还款成功‘%(choice)) 89 else: 90 print(‘输入的信用卡号错误,信用卡号需要为数字‘) 91 else: 92 print(‘输入的密码错误‘) 93 else: 94 print(‘输入的信用卡号不存在‘) 95 96 97 # repayment() 98 ‘‘‘ 99 信用卡取现 100 ‘‘‘ 101 def take_money(): 102 while True: 103 print(‘还款‘.center(20,‘*‘)) 104 with open(creditcard_dic,‘r+‘) as f: 105 creditcard_data = json.loads(f.read()) 106 choice = input(‘请输入取款的账号(退出请输入q): ‘).strip() 107 if choice in creditcard_data.keys(): 108 limit = creditcard_data[choice][‘limit‘] 109 limitcash = creditcard_data[choice][‘limitcash‘] 110 print(‘卡号%s,可取现的额度为%s‘%(choice,limitcash)) 111 if limit >= limitcash: 112 print(‘可取的现金为%s‘%(limitcash)) 113 cash = input(‘请输入要取现的金额(手续费5%): ‘).strip() 114 if cash.isdigit(): 115 cash = int(cash) 116 # limitcash = limitcash - cash * 0.05 117 if cash <= limitcash: 118 if cash > 0: 119 password = input(‘请输入信用卡号%s密码: ‘%(choice)).strip() 120 if password == creditcard_data[choice][‘password‘]: 121 limitcash = int(limitcash - cash * 0.05) 122 limit = int(limit - cash * 0.05) 123 cash_money = cash * 0.05 124 creditcard_data[choice][‘limitcash‘] = limitcash 125 creditcard_data[choice][‘limit‘] = limit 126 dic = json.dumps(creditcard_data) 127 f.seek(0) 128 f.write(dic) 129 take_money = [str(choice),‘信用卡取现‘,str(cash),‘手续费‘,str(cash_money)] 130 msg = ‘***‘.join(take_money) 131 logger.debug(msg) 132 print(‘取现成功‘.center(20,‘*‘)) 133 else: 134 print(‘信用卡号%s输入的密码错误‘%(choice)) 135 else: 136 print(‘取现的金额需要为整数‘) 137 else: 138 print(‘取现的额度超过信用卡额度‘) 139 else: 140 print(‘您已经不可以再取现了‘) 141 else: 142 print(‘信用卡号错误‘) 143 144 # take_money() 145 146 ‘‘‘ 147 信用卡转账 148 ‘‘‘ 149 def transfer(): 150 while True: 151 print(‘转账‘.center(20,‘*‘)) 152 with open(creditcard_dic,‘r+‘) as f: 153 creditcard_data = json.loads(f.read()) 154 choice = input(‘请输入信用卡账号(退出请输入q):‘).strip() 155 if choice == ‘q‘:break 156 if choice in creditcard_data.keys(): 157 limit = creditcard_data[choice][‘limit‘] 158 transfer = input(‘请输入你要转账的账号: ‘).strip() 159 if transfer.isdigit(): 160 if transfer in creditcard_data.keys(): 161 money = input(‘请输入要转账的金额: ‘).strip() 162 if money.isdigit(): 163 money = int(money) 164 my_pwd = input(‘请输入转账的密码: ‘).strip() 165 if my_pwd in creditcard_data[transfer][‘password‘]: 166 if money <= limit: 167 creditcard_data[choice][‘limit‘] -= money 168 creditcard_data[choice][‘limitcash‘] -= money 169 creditcard_data[transfer][‘limit‘] += money 170 creditcard_data[transfer][‘limitcash‘] += money 171 print(‘转账成功‘.center(20,‘*‘)) 172 transfer_data =[str(choice),‘信用卡转账‘,str(money)] 173 msg = ‘***‘.join(transfer_data) 174 logger.debug(msg) 175 dic = json.dumps(creditcard_data) 176 f.seek(0) 177 f.write(dic) 178 else: 179 print(‘转账的金额超过限额‘) 180 else: 181 print(‘信用卡密码错误‘) 182 else: 183 print(‘输入的金额需要为数字‘) 184 else: 185 print(‘输入的转账账号不存在‘) 186 else: 187 print(‘转账的账号出错‘) 188 else: 189 print(‘输入的信用卡号不存在‘) 190 191 # transfer() 192 193 194 ‘‘‘ 195 冻结信用卡 196 ‘‘‘ 197 def lock_creditcard(): 198 while True: 199 print(‘冻结信用卡‘.center(20,‘*‘)) 200 with open(creditcard_dic,‘r+‘) as f: 201 creditcard_data = json.loads(f.read()) 202 # print(type(creditcard_data)) 203 for key in creditcard_data.keys(): 204 if creditcard_data[key][‘locked‘] == False: 205 print(‘信用卡%s未冻结‘%(key)) 206 else: 207 print(‘信用卡%s已经冻结‘%(key)) 208 choice = input(‘请输入要进行冻结的信用卡(请回答是或者否): ‘).strip() 209 if choice == ‘是‘: 210 lock_card = input(‘请输入要冻结的信用卡号: ‘).strip() 211 if lock_card in creditcard_data.keys(): 212 if creditcard_data[lock_card][‘locked‘] == False: 213 creditcard_data[lock_card][‘locked‘] = True 214 dic = json.dumps(creditcard_data) 215 f.seek(0) 216 f.write(dic) 217 else: 218 print(‘信用卡%s已经冻结‘%(lock_card)) 219 else: 220 print(‘要进行冻结的信用卡不存在‘) 221 elif choice == ‘否‘: 222 break 223 224 # lock_creditcard() 225 ‘‘‘ 226 信用卡绑定 227 ‘‘‘ 228 def link_creditcard(): 229 while True: 230 print(‘信用卡绑定‘.center(20,‘*‘)) 231 with open(user_dic,‘r+‘) as f: 232 user_data = json.loads(f.read()) 233 user_name = input(‘请输入需要绑定的信用卡的用户名: ‘).strip() 234 if user_name in user_data.keys(): 235 creditcard = user_data[user_name][‘creditcard‘] 236 if creditcard == False: 237 print(‘用户当前的账号%s‘%(user_name)) 238 print(‘用户%s未绑定信用卡‘%(user_name)) 239 else: 240 print(‘用户%s已经绑定信用卡‘%(user_name)) 241 break 242 creditcard_new = input(‘请输入您的信用卡号: ‘).strip() 243 if creditcard_new.isdigit() and len(creditcard_new) == 6: 244 with open(creditcard_dic,‘r+‘) as f1: 245 creditcard_data = json.loads(f1.read()) 246 if creditcard_new in creditcard_data.keys(): 247 user_data[user_name][‘creditcard‘] = creditcard_new 248 dict = json.dumps(user_data) 249 f.seek(0) 250 f.write(dict) 251 print(‘信用卡%s绑定成功‘%(creditcard_new)) 252 else: 253 print(‘输入的信用卡号不存在‘) 254 else: 255 print(‘输入的信用卡号不符合要求‘) 256 # link_creditcard() 257 258 259 if __name__ == ‘__main__‘: 260 creditcard_data() 261 look_record() 262 repayment() 263 take_money() 264 transfer() 265 lock_creditcard()
1 #Author wangmengzhu 2 import logging 3 import os 4 BASE_PATH = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 5 user_water = BASE_PATH + r‘\log\water_record.txt‘ 6 print(user_water) 7 ‘‘‘ 8 日志模块 9 ‘‘‘ 10 def get_logger(): 11 fh = logging.FileHandler(user_water) 12 logger = logging.getLogger() 13 logger.setLevel(logging.DEBUG) 14 #写入文件中的格式 15 fm = logging.Formatter(‘%(asctime)s --- %(message)s‘) 16 logger.addHandler(fh) 17 fh.setFormatter(fm) 18 return logger 19 get_logger()
1 #Author wangmengzhu 2 import json 3 import os 4 from creditcard import link_creditcard 5 from auth import creditcard_auth 6 from creditcard import link_creditcard 7 from log import get_logger 8 BASE_PATH = os.path.dirname(os.path.dirname(__file__)) 9 ‘‘‘ 10 数据库绝对路径 11 ‘‘‘ 12 shopping_car = BASE_PATH + r‘/db/购物车.txt‘ 13 shopping_lst = BASE_PATH + r‘/db/商品列表.txt‘ 14 user_water = BASE_PATH + r‘/log/water_record.txt‘ 15 creditcard_dic = BASE_PATH + r‘/db/creditcard_data.txt‘ 16 user_dic = BASE_PATH + r‘/db/user_data.txt‘ 17 logger = get_logger() 18 19 ‘‘‘ 20 购物商城 21 ‘‘‘ 22 def shopping_mall(): 23 shopping_list = [] 24 pro_list = [] 25 with open(shopping_lst,‘r+‘) as f: 26 for item in f: 27 pro_list.append(item.strip(‘\n‘).split()) 28 def pro_inf(): 29 for index,item in enumerate(pro_list): 30 print(‘%s,%s,%s‘%(index + 1,item[0],item[1])) 31 while True: 32 print(‘商店已经有的商品‘.center(20,‘*‘)) 33 pro_inf() 34 choice = input(‘请输入要购买的商品号(退出输入q): ‘).strip() 35 if choice == ‘q‘:break 36 if choice.isdigit(): 37 choice = int(choice) 38 if choice <= len(pro_list) and choice >= 0: 39 pro_item = pro_list[choice - 1] 40 print(‘商品%s加入购物车‘%(pro_item[0])) 41 shopping_list.append(pro_item) 42 shopping_data = [‘商品‘,str(pro_item[0]),‘价格‘,str(pro_item[1])] 43 msg = ‘***‘.join(shopping_data) 44 logger.debug(msg) 45 else: 46 print(‘购买的商品号不存在‘) 47 else: 48 print(‘请输入正确的商品号‘) 49 50 51 # shopping_mall() 52 53 ‘‘‘ 54 查看购物记录 55 ‘‘‘ 56 def cat_record(): 57 while True: 58 with open(user_dic,‘r+‘) as f: 59 user_data = json.loads(f.read()) 60 choice = input(‘请输入你要查看购物记录的用户名: ‘).strip() 61 if choice in user_data.keys(): 62 print(‘购物记录‘.center(20,‘*‘)) 63 with open(user_water,‘r‘) as f: 64 for i in f: 65 if choice in i: 66 print(i.strip()) 67 else: 68 print(‘输入的用户名不存在‘) 69 70 # cat_record() 71 72 ‘‘‘ 73 购物结算 74 ‘‘‘ 75 def pay(): 76 while True: 77 print(‘购物结算‘.center(20,‘*‘)) 78 with open(shopping_car,‘r+‘) as f: 79 data = json.loads(f.read()) 80 if data != []: 81 print(‘购物清单‘.center(20,‘*‘)) 82 for index,item in enumerate(data): 83 print(‘%s,%s,%s‘%(index,item[0],item[1])) 84 money = sum(int(i[1]) for i in data) 85 else: 86 print(‘你还没有消费过!‘) 87 break 88 choice = input(‘是否对当前购物车的商品进行结算(回答是或者否): ‘).strip() 89 if choice == ‘否‘:break 90 if choice == ‘是‘: 91 creditcard_auth() 92 user_name = input(‘请输入要进行结算的用户名: ‘).strip() 93 with open(user_dic,‘r+‘) as f1: 94 user_data = json.loads(f1.read()) 95 if user_name in user_data.keys(): 96 user_creditcard = user_data[user_name][‘creditcard‘] 97 if user_creditcard ==False: 98 print(‘您还没有绑定信用卡,请先绑定信用卡‘) 99 link_creditcard() 100 else: 101 with open(creditcard_dic,‘r+‘) as f2: 102 creditcard_data = json.loads(f2.read()) 103 password = input(‘请输入支付密码: ‘).strip() 104 if password == creditcard_data[user_creditcard][‘password‘]: 105 limit = creditcard_data[user_creditcard][‘limit‘] 106 limitcash = creditcard_data[user_creditcard][‘limitcash‘] 107 limit_new = limit - money 108 limitcash_new = limitcash - money 109 if limit_new >= 0: 110 creditcard_data[user_creditcard][‘limit‘] = limit_new 111 creditcard_data[user_creditcard][‘limitcash‘] = limitcash_new 112 shop_data = [user_name,str(user_creditcard),‘信用卡结账‘,str(money)] 113 msg = ‘***‘.join(shop_data) 114 logger.debug(msg) 115 dict = json.dumps(creditcard_data) 116 f2.seek(0) 117 f2.write(dict) 118 print(‘支付成功‘) 119 120 else: 121 print(‘超过支付额度‘) 122 else: 123 print(‘密码错误‘) 124 125 else: 126 print(‘用户名不存在‘)
1 #Author wangmengzhu 2 import os 3 import json 4 BASE_PATH = os.path.dirname(os.path.dirname(__file__)) 5 user_dic = BASE_PATH + r‘/db/user_data.txt‘ 6 user_blacklist = BASE_PATH + r‘/db/黑名单.txt‘ 7 ‘‘‘ 8 创建新用户 9 ‘‘‘ 10 def new_user(address = ‘None‘,locked = False,creditcard = False): 11 while True: 12 print(‘创建用户‘.center(20,‘*‘)) 13 with open(user_dic,‘r+‘) as f: 14 user_data = json.loads(f.read()) 15 for key in user_data: 16 print(‘系统已经有得用户%s‘%(key)) 17 choice = input(‘是否要创建用户(是或者否): ‘).strip() 18 if choice == ‘否‘: 19 continue 20 if choice == ‘是‘: 21 user_name = input(‘请输入你要创建得用户名: ‘).strip() 22 user_pwd = input(‘请输入密码: ‘).strip() 23 if user_name not in user_data.keys(): 24 user_data[user_name] = {"status": False, "username": user_name, "password": user_pwd, 25 "creditcard": creditcard, "address": address, "locked":locked } 26 dic = json.dumps(user_data) 27 # f.tell() 28 f.seek(0) 29 f.write(dic) 30 else: 31 print(‘用户名已经存在,请重新输入‘) 32 else: 33 print(‘输入不合法,请重新输入‘) 34 35 ‘‘‘ 36 解锁用户 37 ‘‘‘ 38 def unlock_user(): 39 while True: 40 print(‘解锁用户‘.center(20,‘*‘)) 41 with open(user_dic,‘r+‘) as f: 42 user_data =json.loads(f.read()) 43 for key in user_data: 44 if user_data[key][‘locked‘] == False: 45 print(‘用户%s锁定状态:未锁定‘%(key)) 46 else: 47 print(‘用户%s锁定状态:已锁定‘%(key)) 48 choice = input(‘是否要进行解锁用户(是或者否): ‘).strip() 49 if choice == ‘否‘: 50 break 51 if choice == ‘是‘: 52 unlock_user = input(‘请输入要解锁的用户名: ‘).strip() 53 if unlock_user in user_data.keys(): 54 if user_data[unlock_user][‘locked‘] == True: 55 user_data[unlock_user][‘locked‘] = False 56 dict = json.dumps(user_data) 57 f.seek(0) 58 f.write(dict) 59 print(‘%s用户已经解锁‘%(unlock_user)) 60 else: 61 print(‘%s用户的状态没有被锁定‘%(unlock_user)) 62 else: 63 print(‘%s用户不存在‘%(unlock_user)) 64 65 ‘‘‘ 66 锁定用户 67 ‘‘‘ 68 def lock_user(): 69 while True: 70 print(‘锁定用户‘.center(20,‘*‘)) 71 with open(user_dic,‘r+‘) as f: 72 user_data = json.loads(f.read()) 73 for key in user_data.keys(): 74 if user_data[key][‘locked‘] == False: 75 print(‘%s用户状态:没有被锁定‘%(key)) 76 else: 77 print(‘%s用户状态:已经被锁定‘%(key)) 78 choice = input(‘是否对用户进行锁定(是或者否): ‘).strip() 79 if choice == ‘否‘: 80 break 81 if choice == ‘是‘: 82 lock_user = input(‘请输入你要进行锁定的用户名:‘).strip() 83 if lock_user in user_data.keys(): 84 if user_data[lock_user][‘locked‘] == False: 85 user_data[lock_user][‘locked‘] == True 86 dic = json.dumps(user_data) 87 f.seek(0) 88 f.write(dic) 89 print(‘%s用户锁定成功‘%(lock_user)) 90 else: 91 print(‘%s的状态已经被锁定‘%(lock_user)) 92 else: 93 print(‘%s用户不存在‘%(lock_user)) 94 95 ‘‘‘ 96 三次锁定 97 ‘‘‘ 98 def three_lock(): 99 while True: 100 with open(user_blacklist,‘r+‘) as f: 101 balck_user = json.loads(f.read()) 102 user_name = input(‘请输入你的用户名: ‘).strip() 103 if user_name in balck_user.keys(): 104 if balck_user[user_name] == 3: 105 print(‘你的%s已经在黑名单中了‘%(user_name)) 106 continue 107 with open(user_dic,‘r+‘) as f1: 108 user_data = json.loads(f1.read()) 109 if user_name in user_data.keys(): 110 if user_data[user_name][‘status‘] == False: 111 if balck_user[user_name] < 3: 112 user_pwd = input(‘请用户%s输入你的登陆密码‘%(user_name)).strip() 113 if user_pwd == user_data[user_name][‘password‘]: 114 user_data[user_name][‘status‘] = True 115 data = json.dumps(user_data) 116 f1.seek(0) 117 f1.write(data) 118 break 119 else: 120 print(‘%s用户输入的密码错误‘%(user_name)) 121 balck_user[user_name] += 1 122 data = json.dumps(balck_user) 123 f.seek(0) 124 f.write(data) 125 else: 126 print(‘%s用户已经在黑名单中‘%(user_name)) 127 else: 128 print(‘%s用户已经在登陆状态‘%(user_name)) 129 else: 130 print(‘%s用户名不存在‘%(user_name)) 131 if __name__ == ‘__main__‘: 132 new_user(address=‘None‘, locked=False, creditcard=False) 133 lock_user() 134 three_lock()
时间: 2024-12-24 19:26:11