1. 程式人生 > >第三周作業part2

第三周作業part2

utf-8 mon spa one alter pan 增加用戶 als 名單

管理部分:

  1 #!/usr/bin/env python  
  2 #-*- coding:utf-8 -*-
  3 
  4 ‘‘‘管理員功能區域‘‘‘
  5 
  6 import os
  7 def acc_info():
  8     """
  9     :return: 返回account_info.txt中以字典形式{
 10     ‘msj‘: ‘123‘, ‘scg‘: ‘123‘, ‘lhy‘: ‘123‘, ‘egon‘: ‘1234‘, ‘flower‘: ‘123‘, ‘fgh‘: ‘123‘, ‘hua‘: ‘123‘}
 11     """
 12
account_info = {} 13 with open(raccount_info.txt,mode=r,encoding=utf-8) as f: 14 for line in f: 15 line = line.strip(\n) 16 if line != ‘‘: 17 u,v = line.split(|) 18 account_info[u] = v 19 return account_info
20 21 def bk_info(): 22 ‘‘‘ 23 錢和信用額度都是浮點數類型 24 :return: {‘msj‘: {‘pwd‘: ‘123‘, ‘money‘: 4600.0, ‘credit‘: 15000.0}} 25 ‘‘‘ 26 bank_user ={} 27 with open(rbank_info.txt,mode=r,encoding=utf-8) as f: 28 for line in f: 29 line = line.strip(\n) 30 if
line != ‘‘: 31 u,p,m,c = line.split(|) 32 bank_user[u]={pwd:p,money:float(m),credit:float(c)} 33 return bank_user 34 35 def select_type(): 36 while True: 37 choice = input(請輸入你想要輸入的類型 1.shop 2.bank>>:).strip() 38 if choice == 1: 39 return shop 40 elif choice == 2: 41 return bank 42 else: 43 print(非法操作) 44 continue 45 46 def is_name(): 47 type = select_type() 48 if type == shop: 49 user_info = acc_info() 50 else: 51 user_info = bk_info() 52 name = input(please input your username(q to exit)>>:).strip() 53 if name in user_info: 54 return name,type 55 else: 56 return False,name,type 57 58 def input_pwd(): 59 pwd = input(請輸入密碼>>:).strip() 60 return pwd 61 62 63 def check_userinfo(): 64 type = select_type() 65 if type == shop: 66 user_info =acc_info() 67 elif type ==bank: 68 user_info = bk_info() 69 for k,v in user_info.items(): 70 print(用戶名%s%k,信息:%s%v) 71 72 73 74 def add_user(): 75 """ 76 添加賬戶 77 :param name: 78 :param pwd: 79 :param type: 賬戶種類 兩種 account 和 bank 80 :return: 81 """ 82 info = is_name() 83 if False in info: 84 name = info[1] 85 type = info[2] 86 else: 87 print(用戶名已存在) 88 return 89 pwd = input_pwd() 90 if type ==shop: 91 file = account_info.txt 92 user_info =%s|%s\n%(name,pwd) 93 elif type ==bank: 94 file = bank_info.txt 95 user_info =%s|%s|0|15000\n%(name,pwd) 96 else: 97 print(非法操作2) 98 return 99 with open(r%s%file,mode=a,encoding=utf-8) as f: 100 f.write(user_info) 101 102 def alter_credit(): 103 ‘‘‘ 104 改變賬號中的信用額 105 :param name: 用戶名 106 :param money: int 錢 107 :return: 108 ‘‘‘ 109 name =input(請輸入用戶名>>:).strip() 110 user_info = bk_info() 111 if name not in user_info: 112 print(用戶名不存在) 113 return 114 money = input(請輸入金額>>:).strip() 115 if not money.isdigit(): 116 print(輸入的金額不對) 117 return 118 money = float(money) 119 user_info[name][credit]=user_info[name][credit]+money 120 # print(user_info[name][‘money‘]) 121 with open(rbank_info.txt,mode=w,encoding=utf-8) as f: 122 for k,v in user_info.items(): 123 msg = {}|{}|{}|{}\n.format(k,v[pwd],v[money],v[credit]) 124 f.write(msg) 125 126 def r_hmd(type): 127 hmd =[] 128 if type ==shop: 129 file = shop_hmd.txt 130 elif type == bank: 131 file =bank_hmd.txt 132 with open(r%s%file,mode=r,encoding=utf-8) as f: 133 for i in f: 134 i=i.strip(\n) 135 if i != "": 136 hmd.append(i) 137 return hmd 138 139 140 def add_hmd(): 141 type =select_type() 142 name = input(輸入加入黑名單的用戶名>>:).strip() 143 if type ==shop: 144 file = shop_hmd.txt 145 user_inf = acc_info() 146 elif type == bank: 147 file =bank_hmd.txt 148 user_inf = bk_info() 149 if name not in user_inf: 150 print(用戶不存在) 151 return 152 hmd = r_hmd(type) 153 if name in hmd: 154 print(用戶名已存在黑名單) 155 return 156 with open(r%s%file,mode=a,encoding=utf-8) as f: 157 f.write(%s\n%name) 158 print(%s 已加入黑名單) 159 160 161 162 def read_hmd(): 163 type = select_type() 164 res =r_hmd(type) 165 print(res) 166 167 def del_hmd(): 168 type = select_type() 169 name = input(想要刪除的黑名單>>:).strip() 170 hmd = r_hmd(type) 171 if name not in hmd: 172 print(用戶不在黑名單) 173 return 174 if type ==shop: 175 file = shop_hmd.txt 176 elif type == bank: 177 file =bank_hmd.txt 178 with open(r%s%file, mode=r, encoding=utf-8) as rf,179 open(r%s.swap%file, mode=w, encoding=utf-8) as wf: 180 for line in rf: 181 if name == line.strip(\n): 182 line = ‘‘ 183 wf.write(line) 184 os.remove(%s%file) 185 os.rename(%s.swap%file,%s%file) 186 187 188 189 msg = """ 190 1、增加用戶 191 2、增加額度 192 3、查看用戶信息 193 4、增加黑名單 194 5、查看黑名單 195 6、刪除黑名單 196 0、退出 197 """ 198 dic ={ 199 1:add_user, 200 2:alter_credit, 201 3:check_userinfo, 202 4:add_hmd, 203 5:read_hmd, 204 6:del_hmd, 205 } 206 207 def admin(): 208 while True: 209 print(msg) 210 choice = input(請輸入你的操作>>:) 211 if choice == 0: 212 print(exit) 213 return 214 elif choice in dic: 215 dic[choice]() 216 else: 217 print(非法操作) 218 continue 219 220 admin()

第三周作業part2